I am trying to code a problem in Java where I have to execute a bunch of tasks.
Problem
Execute a job which consists of multiple tasks and thos
Your problem seems to be a modified version of the Observer Pattern. The solution below is more general than as it allows dependency on a list of tasks to proceed.
Create a class Task as follows:
class Task{
List partialCompletionSuccessors;//List of tasks dependent on the partial completeion of this Task
List fullCompletetionSuccessors;//List of tasks dependent on the full completeion of this Task
List partialCompletionPredeccessor;//List of tasks that this task depends on their partial completion to start
List fullCompletetionPredeccessor;//List of tasks that this task depends on their full completion to start
private void notifySuccessorsOfPartialCompletion(){
for(Task task: partialCompletionSuccessors){
task.notifyOfPartialCompletion(this);
}
}
private void notifySuccessorsOfFullCompletion(){
for(Task task: fullCompletetionSuccessors){
task.notifyOfPartialCompletion(this);
}
}
private tryToProcceed(){
if(partialCompletionPredeccessor.size() == 0 && fullCompletetionPredeccessor.size()==0 ){
//Start the following task...
....
//When this task partially completes
notifySuccessorsOfPartialCompletion();
//When this task fully completes
notifySuccessorsOfFullCompletion();
}
}
public void notifyOfPartialCompletion(Task task){// A method to notify the following task that a predeccessor task has partially completed
partialCompletionPredeccessor.remove(task);
tryToProcceed();
}
public void notifyOfFullCompletion(Task task){// A method to notify the following task that a predeccessor task has partially completed
fullCompletetionPredeccessor.remove(task);
tryToProcceed();
}
}