class OutPut implements Runnable
{
//Object obj= new Object();
//Lock c = new ReentrantLock ();
private Lock w;
private Place r;
OutPut(Place r,Lock w)
{
this.r=r ;
this.w=w;
}
Condition d2= w.newCondition();
public void run()
{
while (true)
{
w.lock();
while (!r.y )
try
{
d2.await();
r.out();
r.y=false ;
d2.signal();
}
catch (Exception e)
{
}
finally{w.unlock();
}
}
}
}
class LockDemo
{
public static void main(String[] args)
{
Lock c = new ReentrantLock ();
Place p = new Place();
InPut in=new InPut(p,c);
OutPut out = new OutPut(p,c);
Thread t1= new Thread(in );
Thread t2= new Thread(out );
Thread t3= new Thread(in );
Thread t4= new Thread(out );
t1.start();
t2.start();
t3.start();
t4.start();
}
}
程序可以编译通过,但是运行结果提示“空指针异常”!! 我认为问题可能出现在红色代码区域 我这里使用的是锁 而不是同步代码块 求解答~~作者: 火焱 时间: 2012-6-15 13:43
newCondition() ---- Returns a new Condition instance that is bound to this Lock instance.