zoukankan      html  css  js  c++  java
  • 多线程按顺序执行3个方法

    一、使用wait(),notify()  注意Object的这两个方法都必须加锁

    public class WaitNotifyTest {
    
        private int flag;
    
        public synchronized void a() {
            while (flag != 0) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("a");
            flag ++;
            notifyAll();
        }
    
        public synchronized void b() {
            while (flag != 1) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
             System.out.println("b");
            flag ++;
            notifyAll();
        }
    
        public synchronized void c() {
            while (flag != 2) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
            System.out.println("c");
            flag = 0;
            notifyAll();
        }
    
        public static void main(String[] args) {
            WaitNotifyTest waitNotifyTest = new WaitNotifyTest();
    
            TestA testA = new TestA(waitNotifyTest);
            TestB testB = new TestB(waitNotifyTest);
            TestC testC = new TestC(waitNotifyTest);
    
            new Thread(testA).start();
            new Thread(testB).start();
            new Thread(testC).start();
        }
    }
    
    class TestA implements Runnable{
    
        private WaitNotifyTest waitNotifyTest;
    
        public TestA(WaitNotifyTest waitNotifyTest) {
            this.waitNotifyTest = waitNotifyTest;
        }
    
        @Override
        public void run() {
            while (true) {
                waitNotifyTest.a();
            }
        }
    }
    
    class TestB implements Runnable{
    
        private WaitNotifyTest waitNotifyTest;
    
        public TestB(WaitNotifyTest waitNotifyTest) {
            this.waitNotifyTest = waitNotifyTest;
        }
    
        @Override
        public void run() {
            while (true) {
                waitNotifyTest.b();
            }
        }
    }
    
    class TestC implements Runnable{
    
        private WaitNotifyTest waitNotifyTest;
    
        public TestC(WaitNotifyTest waitNotifyTest) {
            this.waitNotifyTest = waitNotifyTest;
        }
    
        @Override
        public void run() {
            while (true) {
                waitNotifyTest.c();
            }
        }
    }

    二、使用Condition  (condition更加的灵活他可以选择想要叫醒的线程)

    public class ConditionTest {
        private int flag;
        private Lock lock = new ReentrantLock();
        private Condition conditionA = lock.newCondition();
        private Condition conditionB = lock.newCondition();
        private Condition conditionC = lock.newCondition();
    
        public void a() {
            lock.lock();
            while (flag != 0) {
                try {
                    conditionA.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("a");
            flag ++;
            conditionB.signal(); //唤醒B
            lock.unlock();
        }
    
        public void b() {
            lock.lock();
            while (flag != 1) {
                try {
                    conditionB.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
             System.out.println("b");
            flag ++;
            conditionC.signal();
            lock.unlock();
        }
    
        public void c() {
            lock.lock();
            while (flag != 2) {
                try {
                    conditionC.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
            System.out.println("c");
            flag = 0;
            conditionA.signal();
            lock.unlock();
        }
    
        public static void main(String[] args) {
            WaitNotifyTest waitNotifyTest = new WaitNotifyTest();
    
            TestA test1 = new TestA(waitNotifyTest);
            TestB test2 = new TestB(waitNotifyTest);
            TestC test3 = new TestC(waitNotifyTest);
    
            new Thread(test1).start();
            new Thread(test2).start();
            new Thread(test3).start();
        }
    }
    
    class Test1 implements Runnable{
    
        private WaitNotifyTest waitNotifyTest;
    
        public Test1(WaitNotifyTest waitNotifyTest) {
            this.waitNotifyTest = waitNotifyTest;
        }
    
        @Override
        public void run() {
            while (true) {
                waitNotifyTest.a();
            }
        }
    }
    
    class Test2 implements Runnable{
    
        private WaitNotifyTest waitNotifyTest;
    
        public Test2(WaitNotifyTest waitNotifyTest) {
            this.waitNotifyTest = waitNotifyTest;
        }
    
        @Override
        public void run() {
            while (true) {
                waitNotifyTest.b();
            }
        }
    }
    
    class Test3 implements Runnable{
    
        private WaitNotifyTest waitNotifyTest;
    
        public Test3(WaitNotifyTest waitNotifyTest) {
            this.waitNotifyTest = waitNotifyTest;
        }
    
        @Override
        public void run() {
            while (true) {
                waitNotifyTest.c();
            }
        }
    }
  • 相关阅读:
    【NX二次开发】修改dlx对话框标题的方法
    【NX二次开发】导入x_t,UF_PS_import_data
    设置NX欢迎界面
    [转]10个顶级的CSS UI开源框架
    [转] 多线程 《深入浅出 Java Concurrency》目录
    [转] JAVA多线程和并发基础面试问答
    [转]StuQ 技能图谱(全套13张)
    [转] MongoDB shell 操作 (查询)
    搜集好的java技术帖子,持续更新,java程序员的要求
    [转]JAVA程序员一定知道的优秀第三方库(2016版)
  • 原文地址:https://www.cnblogs.com/gyli20170901/p/10916174.html
Copyright © 2011-2022 走看看