首页 > 代码库 > java多线程编程(3)买票

java多线程编程(3)买票

1,买票非同步版本

http://www.cnblogs.com/anbylau2130/archive/2013/04/17/3025347.html很详细

 1 public class 多线程2 { 2  3     /** 4      * @param args 5      */ 6     public static void main(String[] args) { 7         // TODO Auto-generated method stub 8         Runnable r=new mythread(); 9         Thread t1=new Thread(r);10         Thread t2=new Thread(r);11         t1.start();12         t2.start();13 14     }15 16 }17 class  mythread implements Runnable18 {19     private int  num=100;20   public void run()21   {22      while(true)23      {24          if(num>0){25              try {26                 Thread.sleep(1000);27             } catch (InterruptedException e) {28                 // TODO Auto-generated catch block29                 e.printStackTrace();30             }31              32              33              System.out.println(Thread.currentThread().getName()+"剩下"+num--);}34          else break;35          36      }37       38   }39     40 41 }
View Code