import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; /** * Created by Administrator on 2017/10/12. */ class Car { private boolean waxOn = false; public synchronized void waxed() { waxOn = true; //Ready to buff notifyAll(); } public synchronized void buffed() { waxOn = false; //ready for another coat of wax notifyAll(); } public synchronized void waitForWaxing() throws InterruptedException { while (waxOn == false) wait(); } public synchronized void waitForBuffing() throws InterruptedException { while (waxOn == true) wait(); } } class WaxOn implements Runnable { private Car car; public WaxOn(Car c) { car = c; } public void run() { while (!Thread.interrupted()) { try { System.out.print("\nWax on! "); TimeUnit.MICROSECONDS.sleep(200); car.waxed(); car.waitForBuffing(); } catch (InterruptedException e) { System.out.print("\nExiting via interrupt"); } System.out.print("\nEnding wax on task"); } } } class WaxOff implements Runnable { private Car car; public WaxOff(Car c) { car = c; } public void run(){ try { while (!Thread.interrupted()){ car.waitForBuffing(); System.out.print("\nWax off"); TimeUnit.MICROSECONDS.sleep(200); car.buffed(); } } catch (InterruptedException e) { System.out.print("\nExiting via interrupt!"); } System.out.print("\nExiting via interrupt"); } } public class WaxOMatic { public static void main(String[] args)throws Exception{ Car car = new Car(); ExecutorService exec = Executors.newCachedThreadPool(); exec.execute(new WaxOff(car)); exec.execute(new WaxOn(car)); TimeUnit.SECONDS.sleep(5); exec.shutdownNow(); } }
運行結果:java
Wax on! Wax off!
Exiting via interrupt!
Exiting via interrupt
Exiting via interrupt
Ending wax on taskWax on! it
運行結果:io
Wax off! Wax on! Wax off!
Ending wax on taskWax on!
Ending wax on taskWax on! Wax off! Wax off! Wax off!
Ending wax on taskWax on! Wax off! Wax off!
Ending wax on taskWax on! Wax off!
Ending wax on taskWax on!
Ending wax on taskWax on! Wax off! Wax off!
Ending wax on taskWax on! Wax off! Wax off!
Ending wax on taskWax on! Wax off!
Ending wax on taskWax on! Wax off!
Ending wax on taskWax on! Wax off!
Ending wax on taskWax on! Wax off!
Ending wax on taskWax on! Wax off!
Ending wax on taskWax on! Wax off! Wax off!
Ending wax on taskWax on! Wax off!
Ending wax on taskWax on! Wax off! Wax off! Wax off!
Ending wax on taskWax on! Wax off!
Ending wax on taskWax on!
Ending wax on taskWax on! Wax off! Wax off! Wax off!
Ending wax on taskWax on! Wax off! Wax off!
Ending wax on taskWax on! Wax off! Wax off!
Ending wax on taskWax on! Wax off! Wax off!
Ending wax on taskWax on! Wax off!
Ending wax on taskWax on!
Ending wax on taskWax on! Wax off! Wax off! Wax off!
Ending wax on taskWax on! Wax off! Wax off!
Ending wax on taskWax on! Wax off!
Ending wax on taskWax on! Wax off! Wax off!
Ending wax on taskWax on! Wax off!
Ending wax on taskWax on!
Exiting via interrupt!
Exiting via interrupt
Exiting via interrupt
Ending wax on taskWax on! class