线程的阻塞及唤醒

demo:

 

为方便展示,这里有两个类 : Tasker 、Customer

 

在Customer中,其构造器需要传入Tasker对象,这样我们才能在后面的代码中捕获当前tasker对象,并唤醒该对象等待的线程。

 

public class Tasker implements Runnable{

	public void doSth() throws InterruptedException{
		synchronized (this) {
			for (int i = 0; i < 10; i++) {
				if(i==5){
					this.wait();
				}
				System.out.println("i = " + i);
			}
		}
	}
	
	public void wake(){
		//唤醒对象
		System.out.println("start to wake up");
		this.notifyAll();
		System.out.println("wake up finished");
	}
	
	@Override
	public void run() {
		//获取对象唤醒对象
		synchronized(this){
			this.wake();
		}
	}
	
	public static void main(String[] args) throws InterruptedException {
		Tasker t = new Tasker();
		Customer cust = new Customer(t);
		new Thread(cust).start();
		Thread.sleep(3000);
		new Thread(t).start();
		
	}
	
}

 

public class Customer implements Runnable {
	
	private Tasker tasker;
	
	public Customer(Tasker tasker){
		this.tasker = tasker;
	}
	
	public void doSth() throws InterruptedException{
		tasker.doSth();
	}

	@Override
	public void run() {
		try {
			doSth();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	
}

 从main方法解读:

声明一个tasker对象并交给声明的customer,customer启动线程,调用当前tasker的doSth方法。该方法线程在执行到某条件处wait()。另一方面,当前对象tasker自己也启动线程,调用自己的wake方法。wake方法唤醒了该对象的所有等待线程。doSth方法执行完毕! jdk中要求,唤醒其他线程的代码需写在 同步方法或同步块中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值