2012-12-09 71 views
0

可能重複:
Testing a multithreaded Java class that runs the threads sequentially關於創建同步機制

請不要把這個問題下面爲重複的一個..!

我開發了一個讓多線程按順序依次運行的類。這個類的claimAccess函數和release Access函數之間的所有應用程序代碼一次只能在一個線程中執行。所有其他線程將在隊列中等待,直到前一個線程完成。請告知我想通過在main()方法本身寫入一段代碼來測試我的類。現在

import java.util.ArrayList; 
import java.util.List; 

public class AccessGate { 
    protected boolean shouldWait = false; 
    protected final List waitThreadQueue = new ArrayList(); 

    /** 
    * For a thread to determine if it should wait. It it is, the thread will 
    * wait until notified. 
    * 
    */ 
    public void claimAccess() { 
     final Thread thread = getWaitThread(); 
     if (thread != null) { 
      // let the thread wait untill notified 
      synchronized (thread) { 
       try { 
        thread.wait(); 
       } catch (InterruptedException exp) { 
       } 
      } 
     } 
    } 

    /** 
    * For a thread to determine if it should wait. It it is, the thread will be 
    * put into the waitThreadQueue to wait. 
    * 
    */ 
    private synchronized Thread getWaitThread() { 
     Thread thread = null; 
     if (shouldWait || !waitThreadQueue.isEmpty()) { 
      thread = Thread.currentThread(); 
      waitThreadQueue.add(thread); 
     } 
     shouldWait = true; 
     return thread; 
    } 

    /** 
    * Release the thread in the first position of the waitThreadQueue. 
    * 
    */ 
    public synchronized void releaseAccess() { 
     if (waitThreadQueue.isEmpty()) { 
      shouldWait = false; 
     } else { 
      shouldWait = true; 
      // give the claimAccess function a little time to complete 
      try { 
       Thread.sleep(10); 
      } catch (InterruptedException exp) { 
      } 

      // release the waiting thread 
      final Thread thread = (Thread) waitThreadQueue.remove(0); 
      synchronized (thread) { 
       thread.notifyAll(); 
      } 
     } 
    } 
} 

我的主要方法是..

public static void main (String args[]) 
{ 

} 

請告知我如何在我的我的主要測試上面的類方法釀出THR線程.. !!請大家指教

+0

請考慮使用'Executors.newSingleThreadExecutor()' – hoaz

+0

@Hoaz非常感謝,請發佈完整的代碼,以便我能夠理解更多,謝謝 – user1881169

回答

1

這應該讓你開始...

public static void main (String args[]) 
{ 
    AccessGate gate = new AccessGate(); 

    // create as many threads as you like 
    Thread t1 = new MyThread(gate); 
    Thread t2 = new MyThread(gate); 

    // start all the threads you created 
    t1.start(); 
    t2.start();   
} 

class MyThread extends Thread { 

    AccessGate gate; 

    public MyThread(AccessGate g) { 
     gate = g; 
    } 

    public void run() { 
     gate.claimAccess(); 
     // Do something or print something. 
     // Could output several statements. 
     // Why not do a sleep as well to see if other threads interrupt 
     // this code section. 
     gate.releaseAccess(); 
    } 
} 
0

考慮使用Executors.newSingleThreadExecutor()。這是一個只有一個線程執行任務的線程池。下一步的任務將開始執行第一個任務完成後,才:

Executor executor = Executors.newSingleThreadExecutor(); 
Future<String> future1 = executor.submit(new Callable<String>() { 
    @Override 
    String call() throws Exception { 
     // my first task 
    } 
}); 
Future<String> future2 = executor.submit(new Callable<String>() { 
    @Override 
    String call() throws Exception { 
     // my second task 
    } 
}); 
... 

您可以通過檢索API未來任務執行的結果,也允許你跟蹤每個作業的狀態。

+0

請告訴我如何將上面的代碼與執行程序轉換,我會真的很感謝你,如果你可以請發佈完整的代碼,因爲我已經事先致謝 – user1881169

+0

你不需要任何蓋茨,只是提交你的任務(Callable實例)給執行者,他們將被按順序執行 – hoaz

+0

非常感謝,但我可以通過你建議的方法從主要方法訪問這個,但我試圖通過另一種方法完全通過重入鎖來使上述類訪問門完成。請告知 – user1881169