多线程二 多线程了解与使用

synchronized 锁有两种

  1. 类对象
  2. 类的实例

第一种:锁类对象,有两种方式,如下:

// 方法一:synchronized 修饰static方法
public static synchronized void test(){
    
    
    System.out.println(Thread.currentThread().getName()+" start ");
    try {
    
    
        TimeUnit.SECONDS.sleep(2);
    } catch (Exception e) {
    
    
        e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName()+" end ");
}
// 方法二:synchronized锁class对象
public void test2(){
    
    
    synchronized (getClass()) {
    
    
        try {
    
    
            TimeUnit.SECONDS.sleep(2);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+" run ");
    }
}

public static void main(String[] args) {
    
    
    TestThread10 t = new TestThread10();
    new Thread(()->TestThread10.test(),"线程 1 ").start();
    new Thread(()->t.test2(),"线程 2 ").start();
}

其结果如下:

结果1

在线程1,也就是先启动线程1且等线程1走完,才执行线程2;

锁类,就可以理解为,在类对象上加了一把锁,所有加锁的方法都需要等待上一把锁的释放才能执行

第二种:类的实例

只要对同一个实例对象加锁了,才能实现线程同步,如下:

public void test2(){
    
    
    synchronized (this) {
    
    
        try {
    
    
            TimeUnit.SECONDS.sleep(2);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+" run ");
    }
}

synchronized异常捕获

使用synchronized的时候,出现异常一定要处理,不然他会自动释放锁
它的机制是手动加锁,自动释放锁。下面看一个例子,在异常的地方一定要处理异常,不然就会想下面代码中的线程1,会被释放掉。

private Integer c = 0;

@Override
public void run() {
    
    
  count();
}

private synchronized void count(){
    
    
    System.out.println(Thread.currentThread().getName()+" start。。。");
    while (true) {
    
    
        System.out.println(Thread.currentThread().getName()+" count="+c++);
        try {
    
    
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        if (c == 5) {
    
    
            int i = 1/0;
        }
    }

}

public static void main(String[] args) {
    
    
    TestThread3 t = new TestThread3();
    Thread t1 = new Thread(t, "线程 1");
    Thread t2 = new Thread(t, "线程 2");
    t1.start();
    t2.start();
}


图形2

主线程和子线程

线程分用户线程守护线程

main方法其实是一个主线程,在操作系统启动java.exe后,是开启了一个进程,然后进程启动main线程,main线程有启动其他线程。

  • 守护线程:和主线程一起结束(主线程结束,守护线程也结束)
  • 用户线程(非守护线程):但所有的用户线程结束,主线程结束
t2.setDaemon(true);
t2.start();

volatile的作用

可以看这位博主的博客:Java中Volatile关键字详解 - 郑斌blog - 博客园 (cnblogs.com)

它有两个功能:

  1. 线程间的可见性
  2. 防止指令重排序

注意:

可见性不代表原子性,它只是能够让其他线程能够实时查看到最新值,而其他操作,它不保证。

指令重排序,这个有点深奥,可以先暂时忽略

notify是随机启动等待线程中的一个

notify是随机启动等待线程中的一个,并且跟线程优先级无关,且 wait和notify方法要在同一把lock的情况下使用;还有一点是lock.wait 阻塞还后会把锁让出给需要的线程,然而,在其他线程执行完后,调用lock.notify(),唤醒等待的线程,但是在当前锁里的代码没执行完,不会释放掉锁。

简单场景模拟:

一个固定容量同步容器,拥有put和get方法,以及getCount方法,能够支持两个生产者线程以及10个消费者线程的阻塞调用。

public class TestThread8 {
    
    
private final LinkedList list = new LinkedList();

private final int MAX = 10;

private int count = 0;

public synchronized void put(Object o) {
    
    
    while (list.size() == MAX) {
    
    
        try {
    
    
            // 在这里等待;的那个调用notify时会从这里继续执行
            this.wait();
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
    }
    list.add(o);
    count++;
    // 启动所有线程,包括生产者,随机的
    this.notifyAll();
}

public synchronized void get() {
    
    
    while (list.size() == 0) {
    
    
        try {
    
    
            this.wait();
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
    }
    list.removeLast();
    count--;
    this.notifyAll();
}

public int getCount() {
    
    
    return list.size();
}

public static void main(String[] args) {
    
    
    TestThread8 t = new TestThread8();

    for (int i = 0; i < 2; i++) {
    
    
        new Thread(() -> {
    
    
            int j = 0;
            while (true) {
    
    
                t.put(Thread.currentThread().getName() + " put " + t.getCount());
                System.out.println(Thread.currentThread().getName() + " put " + t.getCount());
            }
        }).start();
    }

    for (int i = 0; i < 10; i++) {
    
    
        new Thread(() -> {
    
    
            while (true) {
    
    
                t.get();
                System.out.println(Thread.currentThread().getName() + " get " + t.getCount());
            }
        }).start();
    }
}
}

猜你喜欢

转载自blog.csdn.net/qq_28911061/article/details/129348273