java - How to run concurrent job with dependent tasks? -


i have situation need work on

i have class has send method, example

@singleton class sendreport {   public void send() {} } 

the send method called user click on web page, , must return immediately, must start sequence of tasks take time

send  ->|    | |-> task1  <-|      |         <-|           |          |-> task2 (can start when task1 completes/throws exception)         <-|           |          |-> task3 (can start when task2 completes/throws exception)         <-| 

i new java concurrent world , reading it. per understanding, need executor service , submit() job(task1) process , future continue.

am correct?

the difficult part me understand , design is
- how , handle exceptions such task?
- far see, have like?

    executorservice executorservice = executors.newfixedthreadpool(1);     future futuretask1 = executorservice.submit(new callable(){     public object call() throws exception {         system.out.println("doing task1");         return "task1 result";     }     });     if (futuretask1.get() != null) {     future futuretask2 = executorservice.submit(new callable(){     public object call() throws exception {         system.out.println("doing task2");         return "task2 result";     }     }     ... , on task 3 

is correct? if yes, there better recommended way?

thanks

if have line of tasks need called on completion of previous 1 stated , discussed in previous answers don't think need multiple threads @ all.

if have pool of tasks , of them needs know outcome of task while others don't care can come dependent callable implementation.

public class dependentcallable implements callable {  private final string name; private final future pre;  public dependentcallable(string name, future pre) {     this.name = name;     this.pre = pre; }  @override public object call() throws exception {     if (pre != null) {         pre.get();         //pre.get(10, timeunit.seconds);     }     system.out.println(name);     return name; } 

a few other things need take care of based on code in question, rid of future.gets in between submits stated in previous replies. use thread pool size of @ least greater depth of dependencies between callables.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -