文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>java三个线程同步...

java三个线程同步...

时间:2010-08-19  来源:hero821

class a extends Thread{
private Kong k;
a(Kong k)
           {
                   this.k=k;
}
public void run()
{
     synchronized(k)
     {
      if(k.flag==false)
      {
       k.horsefly=0;
       k.flag=true;
       try{
        k.wait();
       }
       catch(Exception e){}
      }//三个线程的起始点
      while(k.horsefly!=15){
       try{
        k.wait();
       }
       catch(Exception e){}
      }
      while(true){
       k.horsefly=0;
       System.out.println(k.horsefly);
       for(int i=0;i<100000000;i++);
       k.notifyAll();
       try{
        k.wait();
       }
       catch(Exception e){}
       while(k.horsefly!=15){
          try{
        k.wait();
          }
          catch(Exception e){}
              }
      }
     }
}
}

class b extends Thread{
private Kong k;
b(Kong k){this.k=k;}
public void run()
{
     synchronized(k)
     {
      if(k.flag==false)
      {
       try{
        k.wait();
       }
       catch(Exception e){}
      }
      while(k.horsefly!=0){
       try{
        k.wait();
       }
       catch(Exception e){}
      }
      while(true){
       k.horsefly=10;
       System.out.println(k.horsefly);
       for(int i=0;i<100000000;i++);
       k.notifyAll();
       try{
        k.wait();
       }
       catch(Exception e){}
       while(k.horsefly!=0){
          try{
        k.wait();
          }
          catch(Exception e){}
              }
      }
     }
}
}

class c extends Thread{
private Kong k;
c(Kong k){this.k=k;}
public void run()
{
     synchronized(k)
     {
      if(k.flag==false)
      {
       try{
        k.wait();
       }
       catch(Exception e){}
      }
      while(k.horsefly!=10){
       try{
        k.wait();
       }
       catch(Exception e){}
      }
      while(true){
       k.horsefly=15;
       System.out.println(k.horsefly);
       for(int i=0;i<100000000;i++);
       k.notifyAll();
       try{
        k.wait();
       }
       catch(Exception e){}
       while(k.horsefly!=10){
          try{
        k.wait();
          }
          catch(Exception e){}
              }
      }
     }
}
}

public class Kong{
public int horsefly;
boolean flag=false;
public static void main(String args[])
{
     Kong k=new Kong();
     a a1=new a(k);
     b b1=new b(k);
     c c1=new c(k);
     a1.start();
     b1.start();
     c1.start();
}
}

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载