2016-11-19 66 views
-2
import java.util.concurrent.locks.ReentrantLock; 

class Displayx 
{ 
    public void wish(String name) 
    { 
     ReentrantLock lock = new ReentrantLock(); 
     //using locks 
     lock.lock(); 
      for (int i = 0; i < 10; i++) 
      { 
       System.out.print("Good Morning : "); 
       try { 
        Thread.sleep(2000); 
       } catch (InterruptedException e) { 
        System.out.println("I got intruppted"); 
       } 
       System.out.println(name); 
      } 
     lock.unlock(); 
    } 
} 
class MyThreadex2 extends Thread 
{ 
    Displayx d; 
    String name; 
    public MyThreadex2(Displayx d, String name) 
    { 
     this.d = d; 
     this.name = name; 
    } 
    @Override 
    public void run() 
    { 
     d.wish(name); 
    } 
} 
public class ReentrantLockDemo1 { 

    public static void main(String[] args) 
    { 
     Displayx d = new Displayx(); 
     MyThreadex2 mt1 = new MyThreadex2(d, "SHOAIB"); 
     MyThreadex2 mt2 = new MyThreadex2(d, "RAHUL"); 

     mt1.start(); 
     mt2.start(); 

    } 

} 

輸出我得到是而不是使用同步關鍵字我正在使用鎖定概念,但我沒有得到輸出,因爲我在同步關鍵詞中獲得?

Good Morning : Good Morning : SHOAIB 
Good Morning : RAHUL 
Good Morning : RAHUL 
Good Morning : SHOAIB 
Good Morning : SHOAIB 
Good Morning : RAHUL 
Good Morning : RAHUL 
Good Morning : SHOAIB 
Good Morning : SHOAIB 
Good Morning : RAHUL 
Good Morning : SHOAIB 
Good Morning : RAHUL 
Good Morning : SHOAIB 
Good Morning : RAHUL 
Good Morning : RAHUL 
Good Morning : SHOAIB 
Good Morning : SHOAIB 
Good Morning : RAHUL 
Good Morning : RAHUL 
SHOAIB 
+1

使用**相同**'Lock'?就像你需要在同一個對象上「同步」一樣。事實上,在這種情況下,使用'Lock'沒有任何好處 - 當鎖需要交錯時,它會自動進入... –

+0

也許更重要的是,**不要'擴展Thread' **。 (除非你知道你在做什麼,並有很好的理由) –

+0

@BoristheSpider爲什麼不'擴展線程?請鏈接到解釋原因的文章。 – Andreas

回答

0

移動ReentrantLock lock = new ReentrantLock();之外的方法。

ReentrantLock lock = new ReentrantLock(); 
     public void wish(String name) 
     { 
      //using locks 
      lock.lock(); 
      try { 
       for (int i = 0; i < 10; i++) { 
        System.out.print("Good Morning : "); 
        try { 
         Thread.sleep(2000); 
        } catch (InterruptedException e) { 
         System.out.println("I got intruppted"); 
        } 
        System.out.println(name); 
       } 
      } finally { 
       lock.unlock(); 
      } 

     } 
0

在Java中,在該方法內創建的變量可以在同一個線程中訪問(作用域)。

由於您的ReentrantLock對象是在wish()方法內創建的,因此它對每個線程都是本地的。即您實際上爲每個線程創建單獨的lock對象,因爲其中多個線程正在進入關鍵部分代碼(在try塊內)。

class Displayx 
{ 
    ReentrantLock lock = new ReentrantLock();//Use the same lock object 

    public void wish(String name) 
    { 
     //using locks 
     lock.lock(); 
     try { 
      for (int i = 0; i < 10; i++) { 
       System.out.print("Good Morning : "); 
       try { 
        Thread.sleep(2000); 
       } catch (InterruptedException e) { 
        System.out.println("I got intruppted"); 
       } 
       System.out.println(name); 
      } 
     } finally { 
      lock.unlock(); 
     } 
    } 
} 

而且,鮑里斯在註釋中,請確保您的「編碼到界面的」使用RunnableCallable接口,而不是直接延長Thread類。

相關問題