线程入门——线程间的同步-wait()与notifyAll()

本文通过一个打蜡的例子展示了Java中线程同步的概念。使用了synchronized关键字来控制多个线程对同一资源的访问,确保了线程的安全性和正确性。通过ExecutorService执行两个任务:上蜡(WaxOn)和擦蜡(WaxOff),并利用wait和notifyAll方法协调线程间的执行顺序。
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();
    }

}

 

运行结果:

Wax on! Wax off! 
Exiting via interrupt!
Exiting via interrupt
Exiting via interrupt
Ending wax on taskWax on! 

运行结果:

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! 

转载于:https://my.oschina.net/u/560971/blog/1549963

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值