2009-06-10 3 views
0

여러 가지 멱등 (idempotent) 작업을 실행하여 하나의 데이터 배치를 수집하고 있습니다. 수백 번에 걸친 두 가지 작업으로 계산이 많이 지연된다는 것을 알게되었습니다.이상한 멱등 원 작업의 속도 향상

내가 원하는 것은 이러한 작업을보고 격렬하게 지연되면 낙오자를 다시 시작하는 방법입니다.

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. 
    } 
}