1

我是Java中的新手,試圖通過實現學習Java概念。 這裏的ReentrantLock類的理由是理解鎖。即使使用ReentrantLock鎖定第一個線程,也執行第二個線程

我產卵3線程,在這些我只是增加一個全局計數器。 我正在使用鎖保護其他線程的Counter覆蓋。

import java.util.concurrent.locks.ReentrantLock; 

class ReentryHandledSingleThread extends Thread 
{ 
    static long counter = 0; 

    private int myId; 

    private final ReentrantLock myLock = new ReentrantLock(); 

    public ReentryHandledSingleThread(int id) 
    { 
     this.myId = id; 
    } 

    public void incrementTheCounter() 
    { 

     long stackvariable; 
     int i; 

     for (i = 0; i < 10000; i++) 
     { 
      stackvariable = ReentryHandledSingleThread.counter; 
      stackvariable = stackvariable + 1; 
      ReentryHandledSingleThread.counter = stackvariable; 
     } 
     System.out.println("The value from counter is " + ReentryHandledSingleThread.counter); 
     return; 

    } 

    public void run() 
    { 
     System.out.println("Started Thread No. " + this.myId); 
     this.myLock.lock(); 
     { 
      System.out.println("LOCKED Thread No. " + this.myId); 
      this.incrementTheCounter(); 
     } 
     System.out.println("UNLOCKED Thread No." + this.myId); 
     this.myLock.unlock(); 

    } 
} 

public class RentryHandle 
{ 

    public static void main(String[] args) 
    { 
     System.out.println("Started Executing Main Thread"); 
     int noOfThreads = 3; 
     ReentryHandledSingleThread threads[] = new ReentryHandledSingleThread[noOfThreads]; 
     for (int j = 0; j < noOfThreads; j++) 
     { 
      threads[j] = new ReentryHandledSingleThread(j); 
     } 

     for (int j = 0; j < noOfThreads; j++) 
     { 
      threads[j].start(); 
     } 

     for (int j = 0; j < noOfThreads; j++) 
     { 
      try 

      { 
       threads[j].join(); 
      } 
      catch (InterruptedException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
     System.out.println("Finished Executing Main thrread"); 

    } 
} 

從上面的代碼

Started Executing Main Thread 
    Started Thread No. 0 
    LOCKED Thread No. 0 
    Started Thread No. 2 
    LOCKED Thread No. 2 
    The value from counter is 10226 
    UNLOCKED Thread No.0 
    The value from counter is 16165 
    UNLOCKED Thread No.2 
    Started Thread No. 1 
    LOCKED Thread No. 1 
    The value from counter is 26165 
    UNLOCKED Thread No.1 
    Finished Executing Main thrread 

我的預期輸出

Started Executing Main Thread 
    Started Thread No. 0 
    LOCKED Thread No. 0 
    The value from counter is 10000 
    UNLOCKED Thread No.0 
    Started Thread No. 1 
    LOCKED Thread No. 1 
    The value from counter is 20000 
    UNLOCKED Thread No.1 
    Started Thread No. 2 
    LOCKED Thread No. 2 
    The value from counter is 30000 
    UNLOCKED Thread No.2 
    Finished Executing Main thrread 

觀測的輸出我經歷reentrantlock-lock-doesnt-block-other-threads去不過,我在這裏NOTusing

Condition.await()

因此我不能用我實現共同聯繫。 請幫我理解錯誤或瞭解我的實現中的ReentrantLock應用程序,這些應用程序正在導致「已執行的輸出」和已觀察的輸出中的差異。

回答

1

的問題是,每個線程對象都有其自己的鎖(並且能夠鎖定它獨立於什麼所有其他線程正在做的):

private final ReentrantLock myLock = new ReentrantLock(); 

如果你想鎖定跨線程共享,使上述對象static

+0

謝謝先生..使 私人最終ReentrantLock myLock = new ReentrantLock()後, as private static final ReentrantLock myLock = new ReentrantLock(); 我能夠得到預期的產出。 – 2014-10-07 09:15:48

相關問題