zoukankan      html  css  js  c++  java
  • Thread communication java.util.concurrent.locks.Condition 用法(一)

    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class ConditionTest {
    
        public static void main(String[] args) {
            final Bussiness bussiness = new Bussiness();
            final int times = 5;
            Thread a = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < times; i++) {
                        bussiness.forThreadA();
                    }
                }
            });
            a.setName("ThreadA");
    
            Thread b = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < times; i++) {
                        bussiness.forThreadB();
                    }
                }
            });
            b.setName("ThreadB");
            a.start();
            b.start();
    
        }
    
        static class Bussiness {
            private boolean shouldSubRun = true;
            private Lock lock = new ReentrantLock();
            Condition condition = lock.newCondition();
    
            public void forThreadA() {
                lock.lock();
                if (!shouldSubRun) {
                    try {
                        condition.await();
                    } catch (InterruptedException e) {
                    }
                }
                for (int i = 1; i <= 3; i++) {
                    System.out.println(Thread.currentThread().getName() + " " + i);
                }
                shouldSubRun = false;
                condition.signal();
                lock.unlock();
            }
    
            public void forThreadB() {
                lock.lock();
                while (shouldSubRun) {
                    try {
                        condition.await();
                    } catch (InterruptedException e) {
                    }
                }
                for (int i = 1; i <= 2; i++) {
                    System.out.println(Thread.currentThread().getName() + " " + i);
                }
                shouldSubRun = true;
                condition.signal();
                lock.unlock();
            }
        }
    
    }

    output:

    ThreadA 1
    ThreadA 2
    ThreadA 3
    ThreadB 1
    ThreadB 2
    ThreadA 1
    ThreadA 2
    ThreadA 3
    ThreadB 1
    ThreadB 2
    ThreadA 1
    ThreadA 2
    ThreadA 3
    ThreadB 1
    ThreadB 2
    ThreadA 1
    ThreadA 2
    ThreadA 3
    ThreadB 1
    ThreadB 2
    ThreadA 1
    ThreadA 2
    ThreadA 3
    ThreadB 1
    ThreadB 2

  • 相关阅读:
    python set
    python中%d %2d %02d %-2d% %.2d的区别
    python dict(字典)
    python 300本电子书合集
    python tuple元组
    python end用法
    python 找出第二大值
    GPU大百科全书 第二章 凝固生命的光栅化
    GPU大百科全书 第一章:美女 方程与几何
    Notepad++中调试用心lua程序
  • 原文地址:https://www.cnblogs.com/zhonghan/p/3221375.html
Copyright © 2011-2022 走看看