2013-11-14 94 views
-1

我正在做一些作爲大學任務的一部分,所要求的部分內容是模擬線程故障。對於上下文,我正在使用Java SE的執行器服務模擬Java中的線程故障

我環顧了一下SO和Google,但一直沒有找到具體或具體的東西來做這樣的事情。

有沒有人知道或有任何有關如何處理信息或指導的好消息?

+9

_thread failures_是什麼意思? –

+2

你的意思是你的'Runnable'或'Callable'應該在執行動作時拋出一個異常? –

+0

@SotiriosDelimanolis我的歉意,應該更清楚。我如何解釋這個問題是我需要以某種方式在線程池中完成一項任務,以便有隨機失敗的機會。 爲了進一步澄清,這是我得到的 「模擬線程故障並實現適當的監視和恢復機制。」 此外,我不是在找人來做我的工作,只是想法或信息。 – Eogcloud

回答

1

如果你想測試線程如何「失敗」時,他們遇到異常,你可以實現一個Runnable,你可以命令失敗:

public class FailingRunnable implements Runnable { 

    private volatile boolean doFail = false; 

    @Override 
    public void run() { 
     while(!doFail && ! Thread.interrupted()) 
     { 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
       Thread.currentThread().interrupt(); 
      } 
     } 
     throw new RuntimeException("failed"); 
    } 

    public void failOnNextOccasion() { 
     doFail = true; 
    } 

} 

你必須保持在可運行的引用添加之後給執行者,然後在任何時候調用可運行的方法failOnNextOccasion()。就像這樣:

ExecutorService execSrv = Executors.newFixedThreadPool(2); 

    FailingRunnable one = new FailingRunnable(); 
    FailingRunnable two = new FailingRunnable(); 

    execSrv.submit(one); 
    execSrv.submit(two); 

    try { 
     Thread.sleep(5000); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
     Thread.currentThread().interrupt(); 
    } 

    one.failOnNextOccasion(); 
    two.failOnNextOccasion(); 
0

有點複雜的線程有不那麼明顯的錯誤失敗:

public class Test { 

    static class FailerThread implements Runnable { 

    final Object[] objects; 
    final Random random; 
    final int number; 

    public FailerThread(final Object[] objects, final int number) { 
     this.objects = objects; 
     this.random = new Random(); 
     this.number = number; 
    } 

    @Override 
    public void run() { 
     final boolean isWriter = number % 2 == 0; 
     int index = random.nextInt(objects.length); 
     try { 
     while (Thread.interrupted() == false) { 
      synchronized (objects) { 
      if (isWriter) { 
       while (objects[index] == null) { 
       System.out.println(number + ": Index " + index + " is null, waiting..."); 
       objects.wait(); 
       } 
       for (int copyIndex = 0; copyIndex < objects.length; ++copyIndex) { 
       if (objects[copyIndex] == null) { 
        objects[copyIndex] = this.objects[index]; 
       } 
       } 
       objects.notifyAll(); 
      } else { 
       objects[index] = null; 
      } 
      } 

      ++index; 
      if (index >= objects.length) { 
      index = 0; 
      } 
     } 
     } catch (InterruptedException e) { 
     } 
    } 
    } 

    public static void main(String[] args) throws InterruptedException { 
    final Object[] objects = new Object[10]; 
    for (int i = 0; i < objects.length; ++i) { 
     objects[i] = new Object(); 
    } 

    final int NUM_THREADS = 32; 
    final ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS); 
    for (int i = 0; i < NUM_THREADS; ++i) { 
     executor.execute(new FailerThread(objects, i)); 
    } 
    } 
} 

應該立即失敗,但是這樣做的原因是一切,但微不足道。

+1

您正在檢查InterruptedException中的中斷標誌有一個空的catch-block。誰會設置中斷標誌? – mwhs