美文网首页
2020-06-28---线程通讯

2020-06-28---线程通讯

作者: 李霖神谷 | 来源:发表于2020-06-28 14:57 被阅读0次

    1.等待唤醒机制

    package com.shuai.domain;
    
    //一个线程打印偶数一个线程打印奇数
    public class Test {
        public int i=0;
        Object obj = new Object();
    
        public void jishu() {
            synchronized (obj) {
                while (i < 10) {
                    if (i % 2 == 1) {
                        System.out.println("我运行的是奇数" + i);
                        i++;
                        obj.notify();
                    } else {
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    
        public void oushu() {
            synchronized (obj) {
                while (i < 10) {
                    if (i % 2 == 0) {
                        System.out.println("我运行的是偶数" + i);
                        i++;
                        obj.notify();
                    } else {
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            Test test = new Test();
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    test.jishu();
                }
            });
            Thread thread2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    test.oushu();
                }
            });
            thread.start();
            thread2.start();
        }
    
    }
    

    2.一个线程等待所有其他线程执行完毕之后再执行

    package com.shuai.domain;
    
    import java.util.concurrent.CountDownLatch;
    
    /**
     * 教练需要等待所有学生都准备完毕才能够开始训练
     */
    public class Test {
    //    设置一共有三个学生
        CountDownLatch countDownLatch=new CountDownLatch(3);
        public  void  read(){
            System.out.println("我是学生"+Thread.currentThread().getName()+"号");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"我准备好了");
    //减一操作
            countDownLatch.countDown();
    
        }
        public  void coach(){
            System.out.println("等待学生准备");
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("开始训练吧");
        }
        public static void main(String[] args) {
            Test test = new Test();
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    test.read();
                }
            },"1");
            Thread thread2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    test.read();
                }
            },"2");
            Thread thread3 = new Thread(new Runnable() {
                @Override
                public void run() {
                    test.read();
                }
            },"3");
            Thread thread4 = new Thread(new Runnable() {
                @Override
                public void run() {
                    test.coach();
                }
            });
            thread4.start();
            thread.start();
            thread2.start();
            thread3.start();
        }
    
    }
    

    3.一组线程准备完毕之后同时启动

    package com.shuai.domain;
    
    import java.util.Date;
    import java.util.concurrent.BrokenBarrierException;
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.CyclicBarrier;
    
    /**
     * 所有学生准备好之后同时运行
     */
    public class Test {
    //    一共有四个线程
    CyclicBarrier cyclicBarrier=new CyclicBarrier(4);
    public  void read(){
        String name=Thread.currentThread().getName();
        System.out.println(name+"我在准备");
        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        System.out.println(name+"启动完毕"+System.currentTimeMillis());
    }
        public static void main(String[] args) {
            Test test = new Test();
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    test.read();
                }
            },"1");
            Thread thread2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    test.read();
                }
            },"2");
            Thread thread3 = new Thread(new Runnable() {
                @Override
                public void run() {
                    test.read();
                }
            },"3");
            Thread thread4 = new Thread(new Runnable() {
                @Override
                public void run() {
                    test.read();
                }
            });
            thread4.start();
            thread.start();
            thread2.start();
            thread3.start();
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:2020-06-28---线程通讯

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