- package demo1;
 
 - class Ticket implements Runnable
 
 - {
 
 -         private int tick = 100;
 
  
-         Object obj = new Object();
 
 -         boolean flag = true;
 
  
-         public void run()
 
 -         {
 
 -                 if(flag)
 
 -                 {
 
 -                         while(true)
 
 -                         {
 
 -                         this.flag = false;
 
 -                         synchronized([color=Red]obj[/color])
 
 -                         {
 
 -                             if(tick > 0)
 
 -                             {
 
 -                                     try{
 
 -                                     Thread.sleep(100);
 
 -                                     }catch(Exception e)
 
 -                                     {
 
 -                                     }
 
 -                                     System.out.println(Thread.currentThread().getName() + "   code.... " + tick-- );
 
 -                             
 
 -                             }
 
 -                         }
 
 -                         }
 
 -                 }
 
 -                 else
 
 -                         while(true)
 
 -                         show();
 
  
-         }
 
 -         public [color=Red]synchronized[/color] void show()
 
 -         {
 
  
-                         if(tick>0)
 
 -                         {
 
 -                         
 
 -                                 try{
 
 -                                 Thread.sleep(100);
 
 -                                 }catch(Exception e)
 
 -                                 {
 
 -                                 }
 
 -                                 System.out.println(Thread.currentThread().getName() + "   show.... " + tick-- );
 
 -                                 
 
 -                         }
 
 -         }
 
 - }
 
 - class TicketDemo2
 
 - {
 
 -         public static void main(String[] args) 
 
 -         {
 
 -                 Ticket t = new Ticket();
 
 -                 
 
 -                 Thread t1 = new Thread(t); //创建一个线程
 
 -                 Thread t2 = new Thread(t); //创建一个线程
 
  
-                 t1.start();
 
 -                 t2.start();
 
 -         }
 
 - }
 
 
  复制代码 用的不是同一个锁,方法同步用的this,而同步代码块用的却是obj,所以打出了0 |