线程同步

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

    }

}

posted @ 2016-05-12 22:41  榆次老城  阅读(91)  评论(0编辑  收藏  举报