2009-06-10 1 views
0

私は、複数の等価なタスクを実行して、1つのデータバッチを収集しています。私は数百回の作業の中で何回か計算が遅れることが何度もあることを知りました。偶発的な等価タスクの高速化

私が望むのは、これらのタスクを見て、彼らが大幅に遅れてしまった場合には、再びストラグラーを立ち上げる方法です。

Javaでこれを行うための標準ライブラリまたはイディオムはありますか?私は現在、ExecutorService/ExecutorCompletionServiceのペアを使って仕事を終えています。

答えて

2

このタスクを表すFutureオブジェクトにアクセスできる場合は、必要に応じてisDone()cancel()をチェックすることができます。これらの将来のオブジェクトをポーリングし、それに応じて再送信する必要があります。また、InterruptExceptionsを適切に処理する基盤となるRunnablesにも依存します。

1

各タスクへの参照を保持するタイプのタスクマネージャを作成できます。このタスクマネージャは、各タスクの起動とExecutorServiceの管理を担当することができます。各タスクの最初と最後の操作は、タスクの開始と終了をマネージャに登録することです。マネージャは、各タスクを実行するのに要した時間の平均である統計的な画像を構築することができる。

タスクマネージャは、実行中のタスクのリストを定期的にスキャンして、実行中の特定のタスクの平均時間から大幅に逸脱している「異常値」を探します。その後、これらのタスクを取り消して再起動することができます。

以下

public class Task implements Runnable { 
    protected TaskManager manager_ = null; 
    protected String taskClass_ = null; 
    protected String taskId_ = null; 

    protected Task(TaskManager manager, String taskClass) { 
     manager_ = manager; 
     taskClass_ = taskClass; 
    } 

    /* 
     * Override this and perform specific task. 
     */ 
    protected void perform() { } 

    public void run() { 
     try { 
      manager_.taskStarted(this); 
      perform(); 
      manager_.taskCompleted(this); 
     catch(InterruptedException) { 
      manager_.taskAborted(this); 
     } 
     finally { 
     } 
    } 
} 


public class TaskManager { 
    ExecutorService service_ = null; 

    public TaskManager() { 
     service_ = new ExecutorService(); 
     // start the monitoring thread. 
     service_.execute(this); 
    } 

    public void runTask(Task t) { 
     service_.execute(t); 
    } 

    public void taskStarted(Task t) { 

     1. Note the time that this task (with unique id) has started. 
     2. Add time to a hash map. 
     3. Add task to list of executing tasks. 
    } 

    public void taskComplete(Task t) { 
     1. Find the task id in hash map 
     2. note how long it took to execute. 
     3. modify statistics of how long the task took against 
      the task Class Id. 
     4. Remove task from list of executing tasks. 
    } 

    public void taskAborted(Task t) { 
     // just remove the task from list of running tasks 
     // without altering the statistics. 
    } 
    public void run() { 
     1. Go though the list of executing tasks looking for 
      tasks whose current time - start time is outside the 
      time statistics for the task class id. 
     2. cancel the task and start again. 
    } 
} 
...あなたは何ができるかの非常に大まかな概要であります