美文网首页
04.并发编程之线程通信

04.并发编程之线程通信

作者: commence | 来源:发表于2017-05-12 11:19 被阅读7次

    1、线程通信概念

    线程是操作系统中独立的个体,但这些个体如果不经过特殊的处理就不能成为一个整体,线程间的通信就成为整体的必胜方式之一。当线程存在通信指挥,系统间的交互性更强大,在提高CPU利用率的同时还会使开发人员对线程任务在处理的过程中进行有效把握与监控。
    使用wait/notify方法实现线程间的通信。都是Object类的方法

    • wait/notify必须配合synchronized关键字使用,wait方法释放锁,notify方法不释放锁,会导致不实时
    • 采用CountDownLatch可以实现实时通知。不需要加锁,countDown类似于notifyawait类似于wait,实现两个线程一个等待,一个唤醒。
    package demo1;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.management.RuntimeErrorException;
    
    public class ListAdd2 {
        private volatile static List list = new ArrayList();
    
        public void add() {
            list.add("bjsxt");
        }
    
        public int size() {
            return list.size();
        }
    
        public static void main(String[] args) {
            final ListAdd2 list2 = new ListAdd2();
    
            final Object lock = new Object();
    
            Thread t1 = new Thread(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        synchronized (lock) {
                            for (int i = 0; i < 10; i++) {
                                list2.add();
                                System.out.println("当前线程:" + Thread.currentThread().getName() + "添加了一个元素..");
                                Thread.sleep(500);
                                if (list2.size() == 5) {
                                    System.out.println("已经发出通知..");
                                    lock.notify();
                                }
                            }
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
    
            }, "t1");
    
            Thread t2 = new Thread(new Runnable() {
    
                @Override
                public void run() {
                    synchronized (lock) {
                        if (list2.size() != 5) {
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        System.out.println("当前线程:" + Thread.currentThread().getName() + "收到通知,线程停止..");
                        throw new RuntimeException();
                    }
    
                }
    
            }, "t2");
            t2.start();
            t1.start();
        }
    }
    
    package demo1;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.CountDownLatch;
    
    public class ListAdd3 {
        private volatile static List list = new ArrayList();
    
        public void add() {
            list.add("bjsxt");
        }
    
        public int size() {
            return list.size();
        }
    
        public static void main(String[] args) {
            final ListAdd3 list2 = new ListAdd3();
    
            final CountDownLatch countDownLatch = new CountDownLatch(1);
    
            Thread t1 = new Thread(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        for (int i = 0; i < 10; i++) {
                            list2.add();
                            System.out.println("当前线程:" + Thread.currentThread().getName() + "添加了一个元素..");
                            Thread.sleep(500);
                            if (list2.size() == 5) {
                                System.out.println("已经发出通知..");
                                countDownLatch.countDown();
                            }
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
    
            }, "t1");
    
            Thread t2 = new Thread(new Runnable() {
    
                @Override
                public void run() {
                    if (list2.size() != 5) {
                        try {
                            countDownLatch.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("当前线程:" + Thread.currentThread().getName() + "收到通知,线程停止..");
                    throw new RuntimeException();
                }
    
            }, "t2");
            t2.start();
            t1.start();
        }
    }
    

    相关文章

      网友评论

          本文标题:04.并发编程之线程通信

          本文链接:https://www.haomeiwen.com/subject/xzdztxtx.html