Java并发编程——线程通信

内容

1.wait和notify方法(配合synchronize使用,属于Object方法)

 private volatile static List list = new ArrayList();

    public void add() {
        list.add("abc");
    }

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


    public static void main(String[] args) {
        final TestNotify list1 = new TestNotify();
        Object lock = new Object();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    synchronized (lock) {
                        for (int i = 0; i < 23; i++) {
                            list1.add();
                            System.out.println("当前线程" + Thread.currentThread().getName());
//                            Thread.sleep(512);
                            if (list1.size() == 5) {
                                lock.notify();//唤醒另一个线程,但是并不释放锁
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "t1");

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock) {
                    if (list1.size() != 5) {
                        try {
                            lock.wait();//等待并且释放锁
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                System.out.println("当前线程接收到通知" + Thread.currentThread().getName());
               throw new RuntimeException();
            }
        }, "t2");
        t2.start();
        t1.start();
    }

使用wait和notify进行通信,首先notify唤醒但是不释放锁,所以线程进行通信的时候并不实时

2.CountDownLatch,通过await和countdown实现实时通知

  public static void main(String[] args) {
        final TestNotify list1 = new TestNotify();
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < 23; i++) {
                        list1.add();
                        System.out.println("当前线程" + Thread.currentThread().getName());
//                        Thread.sleep(512);
                        if (list1.size() == 5) {
                            System.out.println("当前已经发出通知、、、、、");
                            countDownLatch.countDown();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "t1");

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                if (list1.size() != 5) {
                    try {
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("当前线程接收到通知" + Thread.currentThread().getName());
//                throw new RuntimeException();
            }
        }, "t2");
        t2.start();
        t1.start();
    }

CyclicBarrier使用场景
每一个线程代表一个跑步运动员,当运动员都准备好后才一起出发,只要有一个没有准备好,就大家都等待。
CountDownLatch使用场景
经常用于监听某一些舒适化的操作。等初始化执行完毕之后,通知主线程继续工作。

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页