package jun;import java.util.concurrent.executorservice;import java.util.concurrent.executors; import java.util.concurrent.locks.condition;import java.util.concurrent.locks.lock;import java.util.concurrent.locks.reentrantlock;/** * starts three thread a,b,c and outputs 10 times in ABC order * * @ author xiejunbo * */public class abc {private int cond = 0;/ /Control ABC output Private lock lock = new reentrantlock ();//Mutex to secure thread access through the lock of the JDK private Condition condition = lock.newcondition ();//thread Collaboration Public static void main (String[] args) {abc abc = new abc (); Threada ta = abc.new threada (); THREADB&NBSP;TB&NBSP;=&NBSP;ABC.NEW&NBSP;THREADB (); THREADC&NBSP;TC&NBSP;=&NBSP;ABC.NEW&NBSP;THREADC (); Executorservice executor = executors.newfixedthreadpool (3);//execute for through thread pool (int i = 0; i < 10; i++) {executor.execute (TA); Executor.execute (TB); Executor.execute (TC);} Executor.shutdown ();} Class threada implements runnable {public void run () {lock.lock ();//Locking try { while (True) {if (cond % 3 == 0) {system.out.print (cond + "A "); Cond++;condition.signalall (); break;} Else{try {condition.await ();} catch (interruptedexception e) {e.printstacktrace ();}}} finally {lock.unlock ();//Unlock}}}class threadb implements runnable{public void run () {Lock.lock ();try {while (True) {if (cond % 3 == 1) { System.out.print (cond + "b "); Cond++;condition.signalall (); break;} else {try {condition.await ();} catch (interruptedexception e) {e.printstacktrace ();}}} finally {lock.unlock ();}}} class threadc implements runnable {public void&Nbsp;run () {lock.lock (); Try {while (True) {if (cond % 3 == 2) {System.out.println (cond + "c "); Cond++;condition.signalall (); break;} Else{try {condition.await ();} catch (interruptedexception e) {e.printstacktrace ();}}} finally {lock.unlock ();}}}
Start three thread a,b,c, output 10 times in ABC order