首页 > 编程语言 > 详细

线程同步

时间:2016-05-13 13:47:10      阅读:164      评论:0      收藏:0      [点我收藏+]

A.java

class T implements Runnable

{

   int money=10000; int goods=100;

   Thread t1,t2;

   T()

   {

    t1=new Thread(this);

    t2=new Thread(this);   

    }

 //  public synchronized void run()

   public void run()

   {

      if(Thread.currentThread()==t1)

      {

          int i=100;

          while(i-->0){System.out.println("正在进货付款:"+(--money)); }

          System.out.println("存货="+(++goods));

       }

      else

      {

          int i=100;

          while(i-->0){System.out.println("正在卖货收款:"+(++money)); }

          System.out.println("存货="+(--goods));

 

       } 

    }

}

class A 

{

   public static void main(String args[])

   {   

      T t=new T();

      t.t1.start();

      t.t2.start();

    }

}

线程同步

原文:http://www.cnblogs.com/sxdxjava/p/5487682.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!