WEB开发网      濂界珯濂藉垎浜紒浣犵殑涓€浠藉垎浜槸鎴戜滑鐨勪竴浠藉姩鍔涳紱璇峰垎浜� ---锕�
开发学院WEB开发Jsp wait()和notify()的简单调用程序 阅读

wait()和notify()的简单调用程序

 2008-01-05 09:22:51 来源:WEB开发网 銆�鍑忓皬瀛椾綋澧炲ぇ瀛椾綋銆�  鍏虫敞璋锋倍閿寸殑寰崥
核心提示:public class road{public static void main(String []args){car c=new car();driver d =new driver(c);passenger p =new passenger(c);new Thread(d).start();new Thread(

public class road
{
public static void main(String []args)
{
car c=new car();
driver d =new driver(c);
passenger p =new passenger(c);
new Thread(d).start();
new Thread(p).start();

}
}
class car
{
synchronized public void sleep()
{
try{
wait();
}
catch(Exception e)
{
}
}
synchronized public void week()
{
notify();
}
}



class passenger implements Runnable
{
car c;
public passenger()
{
}
public passenger(car c)
{
this.c=c;
}
public void run()
{
while(true)
{
Thread.yield();
try{
Thread.sleep(3000);
}catch(InterruptedException ie)
{
}
System.out.PRintln("a passenger go on");
c.week();
System.out.println("waiting stop and sleep...");
c.sleep();


}
}

}




class driver implements Runnable
{
private car c;
public driver()
{
}
public driver(car c)
{
this.c=c;
}
public void run()
{
while(true)
{
System.out.println("wait a passenger.......");
c.sleep();
try{
Thread.sleep(3000);
}catch(InterruptedException ie)
{
}
Thread.yield();
try{
Thread.sleep(100);
}catch(InterruptedException ie)
{
}
c.week();
System.out.println(" passenger week and go ");

}
} }

Tags:wait notify 简单

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接