Package Org.rui.thread.block2;import Java.util.concurrent.executorservice;import java.util.concurrent.Executors; Import Java.util.concurrent.timeunit;import Java.util.concurrent.locks.condition;import Java.util.concurrent.locks.lock;import java.util.concurrent.locks.reentrantlock;/** * Use the displayed Lock and condition, * Only in the more difficult multi-thread problem is required to use * * @author Lenovo * *///Wax Wax Electrical automatic Way public class WaxOMatic2 {public static void main (string[] Arg s) throws Interruptedexception {car car = new Car (); Executorservice exec = Executors.newcachedthreadpool (); Exec.execute (new Waxoff (car)); Exec.execute (new Waxon (car)); TimeUnit.SECONDS.sleep (5); Exec.shutdownnow ();//Interrupt All Tasks//Shutdownnow attempt to stop all active tasks being performed, suspend processing of the awaited task, and return to the list of tasks awaiting execution}} Class Car {private lock lock = new Reentrantlock ();p rivate Condition Condition = lock.newcondition ();//management of the task of communication, does not contain any concerns State information//means polishing, waxing processing status Private Boolean Waxon = false;//waxing public/* synchronized */void waxed () {lock.lock (); try {Waxon = True ;//Ready to buff//notifyall (); Condition.signaLAll ();} finally {Lock.unlock ();}} Polished public/* synchronized */void buffed () {lock.lock (); try {Waxon = false;//ready to another coat of wax//notifyall (); c Ondition.signalall ();} finally {Lock.unlock ();}} Wait waxing public/* synchronized */void waitforwaxing () throws Interruptedexception {lock.lock (); try {while (Waxon = = False {//wait ();//suspend this task condition.await ();}} finally {Lock.unlock ();}} Wait polished public/*synchronized*/void waitforbuffing () throws Interruptedexception {Lock.lock (), try {while (Waxon = True {//wait ();//suspend this task condition.await ();}} finally {Lock.unlock ();}}} Class Waxon implements Runnable {private Car car;public Waxon (car c) {car = c;} @Overridepublic void Run () {try {while (! Thread.interrupted ()) {System.out.println ("Wax on!"); TimeUnit.MILLISECONDS.sleep (car.waxed);//waxing car.waitforbuffing ();//Polishing}} catch (Interruptedexception e) { System.out.println ("Exit by Interruption");//E.printstacktrace ();} System.out.println ("Ending Wax on Task");}} Class WaxOFF implements Runnable {private Car car;public Waxoff (car c) {car = c;} @Overridepublic void Run () {try {while (! Thread.interrupted ()) {car.waitforwaxing ();//etc Wax System.out.println ("Wax off!"); TimeUnit.MILLISECONDS.sleep (+); car.buffed ();//Polishing}} catch (Interruptedexception e) {System.out.println ("Exit by Break") ;//E.printstacktrace ();} System.out.println ("Ending Wax Off task");}} /* Output: (95% match) wax off!wax on!... wax on!wax off!wax on!wax off!wax on!wax off!wax on!wax off!wax on!wax off!wax o N!wax off!wax On!wax off! through interrupt exit ending wax off taskending wax on task */
Java threads use the displayed lock and condition