码迷,mamicode.com
首页 > 编程语言 > 详细

线程同步

时间:2016-05-13 13:47:24      阅读:134      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!