|
| 1 | +package com.chen.api.util.thread.study.chapter4.conditionExecute; |
| 2 | + |
| 3 | +import com.chen.api.util.thread.study.chapter2.throwExceptionNoLock.ThreadB; |
| 4 | + |
| 5 | +import java.util.concurrent.locks.Condition; |
| 6 | +import java.util.concurrent.locks.ReentrantLock; |
| 7 | + |
| 8 | +/** |
| 9 | + * 使用condition实现顺序执行 |
| 10 | + * |
| 11 | + * @author : chen weijie |
| 12 | + * @Date: 2018-05-21 01:11 |
| 13 | + */ |
| 14 | +public class Run { |
| 15 | + volatile private static int nextPrintWho = 1; |
| 16 | + private static ReentrantLock lock = new ReentrantLock(); |
| 17 | + final private static Condition conditionA = lock.newCondition(); |
| 18 | + final private static Condition conditionB = lock.newCondition(); |
| 19 | + final private static Condition conditionC = lock.newCondition(); |
| 20 | + |
| 21 | + |
| 22 | + public static void main(String[] args) { |
| 23 | + |
| 24 | + Thread threadA = new Thread() { |
| 25 | + |
| 26 | + @Override |
| 27 | + public void run() { |
| 28 | + |
| 29 | + try { |
| 30 | + lock.lock(); |
| 31 | + while (nextPrintWho != 1) { |
| 32 | + conditionA.await(); |
| 33 | + } |
| 34 | + |
| 35 | + for (int i = 0; i < 3; i++) { |
| 36 | + System.out.println("threadA:" + (i + 1)); |
| 37 | + } |
| 38 | + |
| 39 | + nextPrintWho = 2; |
| 40 | + conditionB.signalAll(); |
| 41 | + |
| 42 | + |
| 43 | + } catch (InterruptedException e) { |
| 44 | + e.printStackTrace(); |
| 45 | + } finally { |
| 46 | + lock.unlock(); |
| 47 | + } |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + Thread threadB = new Thread() { |
| 52 | + |
| 53 | + |
| 54 | + @Override |
| 55 | + public void run() { |
| 56 | + lock.lock(); |
| 57 | + |
| 58 | + try { |
| 59 | + while (nextPrintWho != 2) { |
| 60 | + conditionB.await(); |
| 61 | + } |
| 62 | + |
| 63 | + for (int i = 0; i < 3; i++) { |
| 64 | + System.out.println("threadB:" + (i + 1)); |
| 65 | + } |
| 66 | + nextPrintWho = 3; |
| 67 | + conditionC.signalAll(); |
| 68 | + } catch (InterruptedException e) { |
| 69 | + e.printStackTrace(); |
| 70 | + } finally { |
| 71 | + lock.unlock(); |
| 72 | + } |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + Thread threadC = new Thread() { |
| 77 | + |
| 78 | + @Override |
| 79 | + public void run() { |
| 80 | + |
| 81 | + try { |
| 82 | + lock.lock(); |
| 83 | + while (nextPrintWho != 3) { |
| 84 | + conditionC.await(); |
| 85 | + } |
| 86 | + for (int i = 0; i < 3; i++) { |
| 87 | + System.out.println("threadC:" + (i + 1)); |
| 88 | + } |
| 89 | + |
| 90 | + nextPrintWho = 1; |
| 91 | + conditionA.signalAll(); |
| 92 | + |
| 93 | + } catch (InterruptedException e) { |
| 94 | + e.printStackTrace(); |
| 95 | + } finally { |
| 96 | + lock.unlock(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + }; |
| 101 | + |
| 102 | + |
| 103 | + Thread[] aArray = new Thread[5]; |
| 104 | + Thread[] bArray = new Thread[5]; |
| 105 | + Thread[] cArray = new Thread[5]; |
| 106 | + |
| 107 | + for (int i = 0; i < 5; i++) { |
| 108 | + aArray[i] = new Thread(threadA); |
| 109 | + bArray[i] = new Thread(threadB); |
| 110 | + cArray[i] = new Thread(threadC); |
| 111 | + aArray[i].start(); |
| 112 | + bArray[i].start(); |
| 113 | + cArray[i].start(); |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | +} |
0 commit comments