java - Could I join threads in a better way? -


suppose have multiple runnable instances in program, dispatched executor instance. further, suppose @ point need wait subset of these runnables finish before moving on.

one way following:

public abstract class joinable implements runnable {      private final semaphore finishedlock = new semaphore(1);      @override     public final void run() {         try {             finishedlock.acquireuninterruptibly();             dowork();         } {             finishedlock.release();         }     }      public abstract void dowork();      public void join() {         finishedlock.acquireuninterruptibly();     } } 

implementing classes can override dowork(), rather run(), in order define should done during execution.

the joining process this:

void dostuff() {      executor executor = executors.newcachedthreadpool();      list<joinable> joinables = new linkedlist<joinable>();     // fill joinables implementors of joinable...      list<runnable> others = new linkedlist<runnable>();     // fill others implementors of runnable...      for(joinable joinable : joinables)          executor.execute(joinable);      for(runnable runnable : others)          executor.execute(runnable);      for(joinable joinable : joinables)          joinable.join();      // continue, no matter threads in others to. } 

is way solve problem (is safe?), or there better one?

your current solution not thread safe. there no guarantees executor call run on joinable before call join. thus, in cases, main thread acquire lock before joinable does.

on possible solution instead use countdownlatch if know total number of joinables n, create countdownlatch(n) , pass each instance. when each joinable finished, have call countdown(). main thread calls await() on latch. await() doesn't return until latch count 0.


Comments