4.1.16ReentrantLock让线程顺序执行

来源:互联网 发布:淘宝旺旺账号数字id 编辑:程序博客网 时间:2024/06/10 18:06

package demo;/** * Created by sunyifeng on 17/10/19. */public class F {    volatile public static int nextPrintWho = 1;}
package demo;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;/** * Created by sunyifeng on 17/10/19. */public class Run {    volatile private static int nextPrintWho = 1;    private static ReentrantLock lock = new ReentrantLock();    final private static Condition conditionA = lock.newCondition();    final private static Condition conditionB = lock.newCondition();    final private static Condition conditionC = lock.newCondition();    public static void main(String[] args) {        // 线程A        Thread threadA = new Thread() {            public void run(){                try {                    lock.lock();                    while (nextPrintWho != 1) {                        conditionA.await();                    }                    for (int i = 0; i < 3; i++) {                        System.out.println("ThreadA " + (i + 1));                    }                    nextPrintWho = 2;                    conditionB.signalAll();                } catch (InterruptedException e) {                    e.printStackTrace();                }finally {                    lock.unlock();                }            }        };        // 线程B        Thread threadB = new Thread(){            public void run(){                try {                    lock.lock();                    while (nextPrintWho != 2) {                        conditionB.await();                    }                    for (int i = 0; i < 3; i++) {                        System.out.println("ThreadB " + (i + 1));                    }                    nextPrintWho = 3;                    conditionC.signalAll();                } catch (InterruptedException e) {                    e.printStackTrace();                } finally {                    lock.unlock();                }            }        };        // 线程C        Thread threadC = new Thread(){            public void run(){                try {                    lock.lock();                    while (nextPrintWho != 3) {                        conditionC.await();                    }                    for (int i = 0; i < 3; i++) {                        System.out.println("ThreadC " + (i + 1));                    }                    nextPrintWho = 1;                    conditionC.signalAll();                } catch (InterruptedException e) {                    e.printStackTrace();                } finally {                    lock.unlock();                }            }        };        Thread[] arrA = new Thread[5];        Thread[] arrB = new Thread[5];        Thread[] arrC = new Thread[5];        for (int i = 0; i < 5; i++) {            arrA[i] = new Thread(threadA);            arrB[i] = new Thread(threadB);            arrC[i] = new Thread(threadC);            //            arrA[i].start();            arrB[i].start();            arrC[i].start();        }    }}
执行结果:

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

程序分析:

使用condition对象让线程按照排列顺序执行