| 
 
| 本帖最后由 张吉日 于 2012-8-10 14:30 编辑 
 /*
 为什么我运行这个代码,在cmd上面出现的都是单线程Thread-0的从100-1,为什么不能实现多线程输出
 */
 class  Ticket implements Runnable //实现接口
 {
 private int tick = 100;
 Object obj = new Object();
 public void run()
 {
 while (true)
 {
 synchronized(obj)//线程保护程序代码....
 {
 if (tick>0)
 {
 try{Thread.sleep(10);}catch(Exception e){}
 System.out.println(Thread.currentThread().getName()+"sale:"+ tick--);
 }
 }
 }
 }
 }
 class  TicketDemo2
 {
 public static void main(String[] args)
 {
 Ticket t = new Ticket();
 Thread t1 =new Thread(t);
 Thread t2 =new Thread(t);
 Thread t3 =new Thread(t);
 Thread t4 =new Thread(t);
 t1.start();
 t2.start();
 t3.start();
 t4.start();
 }
 }
 
 | 
 |