咨询下大家:
在毕老师的视频第146讲 多线程(线程间通信-生产者消费者)中
public synchronized void set(String name)
{
if(flag)
try
{
this.wait();
}catch(Exception e)//t1(失去资格) t2(失去资格)
{
e.printStackTrace();
}
this.name = name+"......"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者。。。。"+this.name);
flag = true;
this.notify();
}
public synchronized void out()
{
if(!flag)
try
{
this.wait();
}catch(Exception e)
{
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"...消费者。。。。"+this.name);
flag = false;
this.notify();
}
我想问一下,在同步函数set中 线程 t1 进去后 不出来 t2 还能进的去?为什么?
|
|