java_base4_thread范例.ppt

线程通信 假定线程t1和线程t2共同操纵一个对象s,这两个线程可以通过对象s的wait()和notify()方法来进行的通信。 S 对象 method1() t1 t2 method2() 生产者与消费者线程通信 class Stack { … public synchronized String pop() { this.notifyAll(); while(point==-1){ System.out.println(Thread.currentThread().getName()+: wait); try{ this.wait(); }catch(InterruptedException e){throw new RuntimeException(e);} } String goods = buffer[point]; buffer[point]=null; Thread.yield(); point--; return goods; } public synchronized void push(String goods) { …} } 参见mythread\newproblem\SyncTest.

文档评论(0)

1亿VIP精品文档

相关文档