Collaboration between java threads (displayed Lock and Conditon objects) study notes

There are additional display tools in the java.util.concurrent class library of java SE5 that can be used to rewrite
MaxOMatic.java

The basic class that uses mutual exclusion and allows tasks to be suspended is Condition . You can suspend a task by calling await () on Condition . When an external task changes, which means that a task should continue to execute, you can call signal () to notify this task, thereby waking up a task, or call signalAll () to wake up all of itself on this Condition() Pending tasks ( signalAll() is a safer way than using notifyAll() ).

class Car{
    private Lock lock=new ReentrantLock();
    private Condition condition=lock.newCondition();
	private boolean waxOn=false;
	public synchronized void waxed() {
		waxOn=true;
		condition.signalAll();
	}
	public synchronized void buffed() {
		waxOn=false;
		condition.signalAll();
	}
	public synchronized void waitingForWaxing() throws InterruptedException {
		while(waxOn==false)
			condition.await();
	}
	public synchronized void waitingForbBuffed() throws InterruptedException {
		while(waxOn==true)
			condition.await();
	}
}
class WaxOn implements Runnable{
    private Car car;
    public WaxOn(Car car) {
    	this.car=car;
    }
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			while(!Thread.interrupted()) {
				System.out.println("Wax On!");
				TimeUnit.MILLISECONDS.sleep(200);
				car.waxed();
				car.waitingForbBuffed();
			}
		}catch(InterruptedException e) {
			System.out.println("Exiting via interrupt");
		}
		System.out.println("Ending Wax On task");
	}
	
}
class WaxOff implements Runnable{
	private Car car;
    public WaxOff(Car car) {
    	this.car=car;
    }
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			car.waitingForWaxing();
			System.out.println("Wax Off!");
			TimeUnit.MILLISECONDS.sleep(200);
			car.buffed();
		}catch(InterruptedException e) {
			System.out.println("Exiting via interrupt");
		}
		System.out.println("Ending Wax Off task");
	}
}
public class WaxOMatic {
    
	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub
        Car car=new Car();
        ExecutorService exec=Executors.newCachedThreadPool();
        exec.execute(new WaxOn(car));
        exec.execute(new WaxOff(car));
        TimeUnit.MILLISECONDS.sleep(5000);
        exec.shutdownNow();
	}

}

In Car's constructor, a single Lock will generate a Condition object, which is used to manage communication between tasks . However, this Condition object does not contain information about the processing status, so you need to manage additional information that represents the processing status, namely boolean waxOn .

Each call to the Lock object must be followed by a try-finally clause to ensure that the lock can be released under all circumstances. When using the built-in version, the task must own this lock before it can call await(), signal() or signal().

Guess you like

Origin blog.csdn.net/weixin_43916777/article/details/104431643