爱心区 公益下载 公益文章 人在旅途 小博士乐园

>>首页 -> 公益文章 -> 技术文栏 -> java文栏 -> java 高级 -> wait()和notify()的简单调用程序



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


作者javasqllike 来源lovesqljava 加入时间:2005-9-22
摘要:
wait()和notify()的简单调用程序

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


转载:转载请保留本信息,本文来自
http://www.51dibs.com/info/24609.htm

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

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 ");

}
} }



相关文章

相关软件

客户服务中心信箱:[email protected]