In this case, the scene of waxing and polishing a car is described.
Car is a waxed and polished vehicle that plays the role of shared resources.
Waxoncommand is responsible for the car waxing, waxing need to monopolize the whole car, a part of the wax, wait for polishing, and then hit a part of the wax.
Buffcommand is responsible for polishing the car, polishing needs exclusive whole car, each time will just hit the wax polishing, and then wait for waxing, and then will just hit the wax polishing.
The Waxoncommand and Buffcommand are executed alternately by two threads in each other:
Waxoncommand Waxing, waiting for polishing, buffcommand polishing, waiting for waxing;
Waxoncommand Waxing, waiting for polishing, buffcommand polishing, waiting for waxing;
Waxoncommand Waxing, waiting for polishing, buffcommand polishing, waiting for waxing;
......
Carwaxbuffdemo demonstrates this scenario.
Code implementation:
Car, can change the waxing state of cars, can wait for the change of waxing state. When the output is in the console, the current execution line Cheng is output together.
Public classCar {PrivateBoolean iswaxed =false; PublicSynchronizedvoidwaxed () {iswaxed=true; Print ("car is waxed now"); Notifyall (); } PublicSynchronizedvoidunwaxed () {iswaxed=false; Print ("car is unwaxed now"); Notifyall (); } PublicSynchronizedvoidWaitingforrewax () throws interruptedexception{ while(iswaxed = =true) {wait (); } Print ("OK to Re-wax"); } PublicSynchronizedvoidWaitingforrebuff () throws interruptedexception{ while(iswaxed = =false) {wait (); } Print ("OK to Re-buff"); } Public voidprint (String msg) {System. out. println ("["+ Thread.CurrentThread (). GetName () +" ] "+msg); }}
Waxoncomman, the car wax, mainly two action: 1. Wait can be waxed, 2. Wax.
import Java.util.concurrent.TimeUnit; Public classWaxoncommand implements Runnable {Privatecar Car; PublicWaxoncommand (car car) { This. Car =car; } @Override Public voidrun () {Try { while(true) {car.waitingforrewax (); Print ("Waxon Take action"); TimeUnit.MILLISECONDS.sleep ( -); Car.waxed (); } }Catch(Exception e) {e.printstacktrace (); } Try{TimeUnit.MILLISECONDS.sleep ( -); } Catch(interruptedexception e) {e.printstacktrace (); } Print ("ending Waxon action"); } Public voidprint (String msg) {System. out. println ("["+ Thread.CurrentThread (). GetName () +" ] "+msg); }}
Buffcommand, polishing the car, the main two actions: 1. Wait can be polished, 2. Polishing.
import Java.util.concurrent.TimeUnit; Public classBuffcommand implements runnable{Privatecar Car; PublicBuffcommand (car car) { This. Car =car; } @Override Public voidrun () {Try { while(true) {car.waitingforrebuff (); Print ("Buff Take action"); TimeUnit.MILLISECONDS.sleep ( the); Car.unwaxed (); } }Catch(interruptedexception e) {e.printstacktrace (); } Try{TimeUnit.MILLISECONDS.sleep ( -); } Catch(interruptedexception e) {e.printstacktrace (); } Print ("ending Buff action"); } Public voidprint (String msg) {System. out. println ("["+ Thread.CurrentThread (). GetName () +" ] "+msg); }}
Show the scene of alternating waxing and polishing of the car. Give 1 seconds, let the waxoncommand/buffcomand thread waxing, polishing operation, 1 seconds later, interrupt their operation.
ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;ImportJava.util.concurrent.TimeUnit; Public classCarwaxbuffdemo { Public Static voidMain ()throwsinterruptedexception{Executorservice exec=Executors.newcachedthreadpool (); Car Car=NewCar (); Exec.execute (NewBuffcommand (car)); Exec.execute (NewWaxoncommand (car)); TimeUnit.SECONDS.sleep (1); System.out.println ("Executor stopping"); Exec.shutdownnow (); }}
Output Result:
The thread-1 and thread-2 are alternately waxed and polished before being forcibly interrupted.
[Pool-1-thread-1] OK to Re-Wax [Pool-1-thread-1] Waxon take action [pool-1-thread-1] car is waxed now [pool-1-thread-2] OK to re-Buff [Pool-1-thread-2] Buff take action [pool-1-thread-2] Car is unwaxed now [pool-1-thread-1] OK to re-Wax [Pool-1-thread-1] Waxon take action [pool-1-thread-1] car is waxed now [pool-1-thread-2] OK to re-Buff [Pool-1-thread-2] Buff take action [pool-1-thread-2] Car is unwaxed now [pool-1-thread-1] OK to re-Wax [Pool-1-thread-1] Waxon take action [pool-1-thread-1] car is waxed now [pool-1-thread-2] OK to re-Buff [Pool-1-thread-2] Buff take action [pool-1-thread-2] Car is unwaxed now [pool-1-thread-1] OK to re-Wax [Pool-1-thread-1] Waxon take action [pool-1-thread-1] car is waxed now [pool-1-thread-2] OK to re-Buff [Pool-1-thread-2] Buff take action [pool-1-thread-2] Car is unwaxed now [pool-1-thread-1] OK to re-Wax [Pool-1-thread-1] Waxon take action [pool-1-thread-1] car is waxed now [pool-1-thread-2] OK to re-Buff [Pool-1-thread-2] Buff take action [pool-1-thread-2] Car is unwaxed now [pool-1-thread-1] OK to re-Wax [Pool-1-thread-1] Waxon take action executor stopping java.lang.InterruptedException at java.lang.Object.wait (Native Method) At Java.lang.Object.wait (Object.java:502) at ConcurrencyCooperation.Car.waitingForReBuff (Car.java:29) at ConcurrencyCooperation.BuffCommand.run (Buffcommand.java:17) at Java.util.concurrent.ThreadPoolExecutor.runWorker (Threadpoolexecutor.java:1142) at Java.util.concurrent.threadpoolexecutor$worker.run (Threadpoolexecutor.java:617) at Java.lang.Thread.run (Thread.java:745) Java.lang.InterruptedException:sleep interrupted at Java.lang.Thread.sleep (Native Method) at Java.lang.Thread. Sleep (Thread.java:340) at Java.util.concurrent.TimeUnit.sleep (Timeunit.java:386) at ConcurrencyCooperation.WaxonCommand.run (Waxoncommand.java:20) at Java.util.concurrent.ThreadPoolExecutor.runWorker (Threadpoolexecutor.java:1142) at Java.util.concurrent.threadpoolexecutor$worker.run (Threadpoolexecutor.java:617) at Java.lang.Thread.run (Thread.java:745) [Pool-1-thread-2] Ending Buff action [pool-1-THREAD-1] Ending Waxon action
Resources:
Page 857, Wait () and Notifyall (), Thinking in Java
Simple case of [Java Concurrent] Wait/notifyall