当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:16:00  来源:本站整理

wait()和notify()的简单调用程序[Java编程]

赞助商链接



  本文“wait()和notify()的简单调用程序[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

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


  以上是“wait()和notify()的简单调用程序[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • wait()和notify()的简单调用程序
  • Java多线程筹划情势 - wait/notify机制
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .