现在的位置: 首页 > 综合 > 正文

浅谈生产者消费者问题中的wait()和notify()的使用

2019年08月14日 ⁄ 综合 ⁄ 共 355字 ⁄ 字号 评论关闭
public synchronized WoTou pop() {
     if(index == 0) {
	    try {
		     this.wait();
	    }catch (InterruptedException e) {
		   e.printStackTrace();
		}
	}
	this.notify();
	index--;
	return arrWT[index];
}

关于notify():

void	notify()

Wakes up a single thread that is waiting on this object's monitor.

notifu()函数用来唤醒正在wait的一个线程。

this.wait():

 wait()使得访问当前对象的线程wait!且该对象必须为synchronized锁住,wait与sleep的区别为,wait时锁中的对象可以被访问,而sleep肯定不可以

抱歉!评论已关闭.