java - How to check that @Async call completed in Spring? -


im using @async annotation method execute rsync command. there ten threads calling method @ time. requirement after ten threads complete rsync command execution remaining code should execute not getting how check whether ten threads has executed @async method or not? please tell me way check it

if going return value, should wrap return value standard java se future or spring's asyncresult, implements future also.

something this:

@component class asynctask {   @async   public future<string> call() throws interruptedexception {     return new asyncresult<string>("return value");   } } 

if have in place, in caller like:

public void kickoffasynctask() throws interruptedexception {   future<string> futureresult =  asynctask.call();    //do stuff in parallel    string result = futureresult.get();   system.out.println(result); } 

call futureresult.get() block caller thread , wait until async thread finishes.

optionally can use future.get(long timeout, timeunit unit) if don't want wait forever.

edit:

if don't need return value, still suggest consider returning dummy return value. don't need use anything, use indicate particular thread completed. this:

public void kickoffasynctasks(int execcount) throws interruptedexception {   collection<future<string>> results = new arraylist<>(execcount);    //kick off threads   (int idx = 0; idx < execcount; idx++) {     results.add(asynctask.call());   }    // wait threads   results.foreach(result -> {     try {       result.get();     } catch (interruptedexception | executionexception e) {       //handle thread error     }   });    //all threads finished } 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -