synchronized (queue),獲得當前對象的鎖.queue.notify ();
就是通知在等待當前對象的鎖釋放的線程,說我執行完了,該你了.
queue.wait ();
它處於等待狀態,隨時可能會被別的線程喚醒(invoke object's notify(), notifyAll() method)
就是通知在等待當前對象的鎖釋放的線程,說我執行完了,該你了.
queue.wait ();
它處於等待狀態,隨時可能會被別的線程喚醒(invoke object's notify(), notifyAll() method)
synchronized (queue) {
while (queue.isEmpty ()) {
queue.wait ();
}
Object item = queue.firstElement ();
queue.removeElement (item);
return item;
}
}
中的queue.wait()有什么用,是不是remove()里面的语句等待不运行,等notify()以后再转回来执行remove()中的操作,能否教一下,对object中的wait()不太懂,在这里好象感觉跟线程没有关系一样
synchronized(queue){
while(queue.isEmpty()){
queue.wait();
}
..
queue.removeElement(item);
..
}
进入synchronized时,这段代码就获得了queue对象上的锁,但是如果queue是空的,必然不能再进行delete操作(我搞不懂,为什么不直接return得了?),要等待其它操作往queue上放些东西,但是此时锁被此函数获得,其它对象不能对queue操作,包括向queue里面添加元素。所以只有用wait方法把所获得的queue对象上的释放,并等待其它函数操作完成后发送一个notify才继续...