java.util.concurrent - Waiting for a hierarchy of tasks to complete -
this abstraction of actual problem, hope it's accurate enough explain things.
i'm processing file hierarchy, , i'm processing files asynchronously using java threadpoolexecutor finite number of threads , unbounded queue. starting @ particular directory, add task queue each file within directory. shutdown , await completion.
the tasks despatched using executor.execute(runnable).
the problem when directory contains further directories, 1 of these tasks may spawn further tasks, , these tasks not being executed because @ top level threadpoolexecutor has been shut down.
so question is, in top level thread, how await completion of whole hierarchy of tasks, recognising haven't started yet?
i did abstraction of problem. if described, walk whole hierarchy in orginal parent thread , fire off tasks there. in real problem can't that: it's essential feature of problem spawned child task submits further tasks.
in top level thread can use that:
countdownlatch latch = new countdownlatch(n); executor.execute(new worker(latch, sometask)); . . . n latch.await();
a countdownlatch initialized n can used make 1 thread wait until n threads have completed action, or action has been completed n times. http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/countdownlatch.html
Comments
Post a Comment