java - Threads Execution -
is possible know when thread has completed execution without using of builtin function isalive, join. suppose have 3 threads a, b , c running in parallel. how know whether threads have completed execution.
you can use countdownlatch out it. let's you're making threads executorservice, make like
countdownlatch latch = new countdownlatch(threadscount); executorservice executor = executors.newfixedthreadpool(threadscount); (int x = 0; x < threadscount; x++) { executor.submit(new classname(latch)); } try { latch.await(); } catch (exception ignored){} //all threads done @ point
and in classname should have this:
class classname implements runnable { private countdownlatch latch; //constructor: classname(countdownlatch latch) { this.latch = latch; } //run method: @override void run() { //your thread code... latch.countdown(); } }
so in run-method in classname, need call latch.countdown().
countdownlatch automatically syncronized, don't need think that.
you don't need use exectuorservice in order use latch, example.
Comments
Post a Comment