想用lock写一个线程同步的例子 
但是好像没有锁住操作 下面是我的代码
import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;public class TestThread extends Thread{    Random ran = new Random();    
    static  int i = 0;
    ReentrantLock lock = new ReentrantLock();
    
    public void run()
    {
     lock.lock(); 
     play();
     lock.unlock();
    
    
     }

    public  void play()
    {
     while(true)
     {
     if( i > 50)
     {
     break;
     }
   
     int x = (int)(Math.random()*1000+1);  
     System.out.println(Thread.currentThread()+"**** the indexof is:  "+i+"   the number is:"+x);
     i++;     
    
     }  
    }
    
    public static void main(String[] args)
    {
        new TestThread().start();
        new TestThread().start();
    
     }
    
    
}貌似没有锁住对I的操作下面是代码的执行结果Thread[Thread-1,5,main]**** the indexof is:  0   the number is:137
Thread[Thread-0,5,main]**** the indexof is:  0   the number is:563

Thread[Thread-1,5,main]**** the indexof is:  1   the number is:624
Thread[Thread-0,5,main]**** the indexof is:  2   the number is:324
Thread[Thread-1,5,main]**** the indexof is:  3   the number is:543
Thread[Thread-0,5,main]**** the indexof is:  4   the number is:296
Thread[Thread-1,5,main]**** the indexof is:  5   the number is:171
Thread[Thread-0,5,main]**** the indexof is:  6   the number is:267
Thread[Thread-1,5,main]**** the indexof is:  7   the number is:360
Thread[Thread-0,5,main]**** the indexof is:  8   the number is:126
Thread[Thread-1,5,main]**** the indexof is:  9   the number is:658
Thread[Thread-0,5,main]**** the indexof is:  10   the number is:291
Thread[Thread-1,5,main]**** the indexof is:  11   the number is:72
Thread[Thread-0,5,main]**** the indexof is:  12   the number is:910
Thread[Thread-1,5,main]**** the indexof is:  13   the number is:159
Thread[Thread-0,5,main]**** the indexof is:  14   the number is:453
Thread[Thread-1,5,main]**** the indexof is:  15   the number is:109
Thread[Thread-0,5,main]**** the indexof is:  16   the number is:123
Thread[Thread-1,5,main]**** the indexof is:  17   the number is:23
Thread[Thread-0,5,main]**** the indexof is:  18   the number is:708
Thread[Thread-0,5,main]**** the indexof is:  20   the number is:158
Thread[Thread-0,5,main]**** the indexof is:  21   the number is:451
Thread[Thread-0,5,main]**** the indexof is:  22   the number is:426
Thread[Thread-0,5,main]**** the indexof is:  23   the number is:286
Thread[Thread-1,5,main]**** the indexof is:  22   the number is:715
Thread[Thread-0,5,main]**** the indexof is:  24   the number is:729
Thread[Thread-1,5,main]**** the indexof is:  25   the number is:564
Thread[Thread-0,5,main]**** the indexof is:  26   the number is:495
Thread[Thread-1,5,main]**** the indexof is:  27   the number is:281
Thread[Thread-0,5,main]**** the indexof is:  28   the number is:543
Thread[Thread-1,5,main]**** the indexof is:  29   the number is:599
Thread[Thread-0,5,main]**** the indexof is:  30   the number is:529
Thread[Thread-1,5,main]**** the indexof is:  31   the number is:98
Thread[Thread-0,5,main]**** the indexof is:  32   the number is:866
Thread[Thread-1,5,main]**** the indexof is:  33   the number is:278
Thread[Thread-0,5,main]**** the indexof is:  34   the number is:355
Thread[Thread-1,5,main]**** the indexof is:  35   the number is:76
Thread[Thread-0,5,main]**** the indexof is:  36   the number is:745
Thread[Thread-1,5,main]**** the indexof is:  37   the number is:717
Thread[Thread-0,5,main]**** the indexof is:  38   the number is:755
Thread[Thread-1,5,main]**** the indexof is:  39   the number is:918
Thread[Thread-0,5,main]**** the indexof is:  40   the number is:401
Thread[Thread-1,5,main]**** the indexof is:  41   the number is:448
Thread[Thread-0,5,main]**** the indexof is:  42   the number is:467
Thread[Thread-1,5,main]**** the indexof is:  43   the number is:390
Thread[Thread-0,5,main]**** the indexof is:  44   the number is:775
Thread[Thread-1,5,main]**** the indexof is:  45   the number is:282
Thread[Thread-0,5,main]**** the indexof is:  46   the number is:542
Thread[Thread-1,5,main]**** the indexof is:  47   the number is:97
Thread[Thread-0,5,main]**** the indexof is:  48   the number is:272
Thread[Thread-1,5,main]**** the indexof is:  49   the number is:755
Thread[Thread-0,5,main]**** the indexof is:  50   the number is:481
在上面两处位置出现了这样的东东 希望达人可以帮忙 谢谢了

解决方案 »

  1.   

    有好多玩用的代码package com.dtb.test;import java.util.concurrent.locks.ReentrantLock;public class Test_Thread extends Thread{

    static  int i = 0;
    ReentrantLock lock = new ReentrantLock(); public void run()
    {
    lock.lock();     
    play();
    lock.unlock();
    } public void play()
    {
    while(i<=50)
    {
    int x = (int)(Math.random()*1000+1);          
    System.out.println(Thread.currentThread()+"**** the indexof is:  "+i+"   the number is:"+x);
    i++;              }  
    } public static void main(String[] args) {
    Test_Thread t = new Test_Thread();
    t.start();
    t.start();
    t.start();
    t.start();
    }}
      

  2.   

    发错了代码,不好意思
    package com.dtb.test;import java.util.concurrent.locks.ReentrantLock;public class Test_Thread extends Thread{

    static  int i = 0;
    ReentrantLock lock = new ReentrantLock(); public void run()
    {
    lock.lock();     
    play();
    lock.unlock();
    } public void play()
    {
    while(i<=50)
    {
    int x = (int)(Math.random()*1000+1);          
    System.out.println(Thread.currentThread()+"**** the indexof is:  "+i+"   the number is:"+x);
    i++;              }  
    } public static void main(String[] args) {
    new Test_Thread().start();
    new Test_Thread().start();
    }}
      

  3.   


    import java.util.Random;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;public class TestThread extends Thread{    Random ran = new Random();    
        static  int i = 0;
        static ReentrantLock lock = new ReentrantLock();
        
        public void run()
        {
            lock.lock();     
            play();
            lock.unlock();
            
            
            }
        
        public  void play()
        {
            while(true)
            {
                if( i > 50)
                {
                    break;
                }
           
                int x = (int)(Math.random()*1000+1);          
                System.out.println(Thread.currentThread()+"**** the indexof is:  "+i+"   the number is:"+x);
                i++;             
                
            }  
        }
        
        public static void main(String[] args)
        {
            new TestThread().start();
            new TestThread().start();
            
            }
        
        
    }
      

  4.   

    要锁对i的操作 加锁的位置不对啊 import java.util.concurrent.locks.ReentrantLock;public class Test_Thread extends Thread{
        
        static  int i = 0;
        static ReentrantLock lock = new ReentrantLock();    public void run()
        {       
            play();
        }    public void play()
        {
            while(i<=50)
            {
    lock.lock();  
                int x = (int)(Math.random()*1000+1);          
                System.out.println(Thread.currentThread()+"**** the indexof is:  "+i+"   the number is:"+x);
                i++;             
    lock.unlock();
            }  
        }    public static void main(String[] args) {
            new Test_Thread().start();
            new Test_Thread().start();
        }}
      

  5.   

    楼上的好像也不对吧,你看看
    Thread[Thread-0,5,main]**** the indexof is:  0   the number is:777
    Thread[Thread-0,5,main]**** the indexof is:  1   the number is:504
    Thread[Thread-0,5,main]**** the indexof is:  2   the number is:854
    Thread[Thread-0,5,main]**** the indexof is:  3   the number is:905
    Thread[Thread-0,5,main]**** the indexof is:  4   the number is:813
    Thread[Thread-0,5,main]**** the indexof is:  5   the number is:281
    Thread[Thread-0,5,main]**** the indexof is:  6   the number is:98
    Thread[Thread-0,5,main]**** the indexof is:  7   the number is:880
    Thread[Thread-0,5,main]**** the indexof is:  8   the number is:138
    Thread[Thread-0,5,main]**** the indexof is:  9   the number is:392
    Thread[Thread-0,5,main]**** the indexof is:  10   the number is:434
    Thread[Thread-0,5,main]**** the indexof is:  11   the number is:566
    Thread[Thread-0,5,main]**** the indexof is:  12   the number is:60
    Thread[Thread-0,5,main]**** the indexof is:  13   the number is:694
    Thread[Thread-0,5,main]**** the indexof is:  14   the number is:799
    Thread[Thread-0,5,main]**** the indexof is:  15   the number is:402
    Thread[Thread-0,5,main]**** the indexof is:  16   the number is:1
    Thread[Thread-0,5,main]**** the indexof is:  17   the number is:517
    Thread[Thread-0,5,main]**** the indexof is:  18   the number is:400
    Thread[Thread-0,5,main]**** the indexof is:  19   the number is:112
    Thread[Thread-0,5,main]**** the indexof is:  20   the number is:262
    Thread[Thread-0,5,main]**** the indexof is:  21   the number is:180
    Thread[Thread-0,5,main]**** the indexof is:  22   the number is:338
    Thread[Thread-0,5,main]**** the indexof is:  23   the number is:149
    Thread[Thread-0,5,main]**** the indexof is:  24   the number is:411
    Thread[Thread-0,5,main]**** the indexof is:  25   the number is:316
    Thread[Thread-1,5,main]**** the indexof is:  26   the number is:731
    Thread[Thread-1,5,main]**** the indexof is:  27   the number is:147
    Thread[Thread-1,5,main]**** the indexof is:  28   the number is:203
    Thread[Thread-1,5,main]**** the indexof is:  29   the number is:471
    Thread[Thread-1,5,main]**** the indexof is:  30   the number is:663
    Thread[Thread-1,5,main]**** the indexof is:  31   the number is:258
    Thread[Thread-1,5,main]**** the indexof is:  32   the number is:564
    Thread[Thread-1,5,main]**** the indexof is:  33   the number is:865
    Thread[Thread-1,5,main]**** the indexof is:  34   the number is:588
    Thread[Thread-1,5,main]**** the indexof is:  35   the number is:934
    Thread[Thread-1,5,main]**** the indexof is:  36   the number is:337
    Thread[Thread-1,5,main]**** the indexof is:  37   the number is:357
    Thread[Thread-0,5,main]**** the indexof is:  38   the number is:73
    Thread[Thread-0,5,main]**** the indexof is:  39   the number is:680
    Thread[Thread-0,5,main]**** the indexof is:  40   the number is:140
    Thread[Thread-0,5,main]**** the indexof is:  41   the number is:671
    Thread[Thread-0,5,main]**** the indexof is:  42   the number is:108
    Thread[Thread-0,5,main]**** the indexof is:  43   the number is:504
    Thread[Thread-0,5,main]**** the indexof is:  44   the number is:935
    Thread[Thread-0,5,main]**** the indexof is:  45   the number is:648
    Thread[Thread-0,5,main]**** the indexof is:  46   the number is:535
    Thread[Thread-0,5,main]**** the indexof is:  47   the number is:355
    Thread[Thread-0,5,main]**** the indexof is:  48   the number is:603
    Thread[Thread-0,5,main]**** the indexof is:  49   the number is:852
    Thread[Thread-0,5,main]**** the indexof is:  50   the number is:582
    Thread[Thread-1,5,main]**** the indexof is:  51   the number is:591
    多了 个51
      

  6.   

    应该把lock写到循环外面import java.util.concurrent.locks.ReentrantLock;public class Test_Thread extends Thread{
        
        static  int i = 0;
        static ReentrantLock lock = new ReentrantLock();    public void run()
        {       
            play();
        }    public void play()
        {
         lock.lock();  
            while(i<=50)
            {
               
                int x = (int)(Math.random()*1000+1);          
                System.out.println(Thread.currentThread()+"**** the indexof is:  "+i+"   the number is:"+x);
                i++;             
                
            } 
            lock.unlock();
        }    public static void main(String[] args) {
            new Test_Thread().start();
            new Test_Thread().start();
        }}
      

  7.   

    static ReentrantLock lock = new ReentrantLock();为什么在这里加static呢 ??请高手解答一下。
      

  8.   

    答:楼主的代码问题的核心是:
    new TestThread().start(); //这个线程锁的是自己的ReentrantLock lock 
    new TestThread().start(); //这个线程锁的也是自己的ReentrantLock lock 
    这是两个不同的lock,当然出问题了.
    解决方法:
    方法1)static ReentrantLock lock =...
    这样,两个线程加的同一个锁方法2):产生一个共同的ReentrantLock lock =...;
    然后通过构造器,分别转给两个线程(即:两个线程共用同一个锁)另外:楼主一定要注意:必须用如下格式来写:
    try{
     lock.lock();     
     play();
            }
    finally{
      lock.unlock();
     }
      

  9.   

    答:上面打错了,必须用如下格式来写: 
    lock.lock();    
    try{ 
    play(); 
     } 
    finally{ 
      lock.unlock(); 

      

  10.   

    线程同步确实是个难点。从CODESOSO上,找到了一些同步的技巧和方法