multithreading - How can we call multiple threads in Java? -
is there way call thread after finishing work of thread? want call 3 threads back work gets finished of previous thread.
i'm trying this
public class testthreads { public static void main (string [] args) { myrunnable r = new myrunnable(); thread first = new thread(r); thread second = new thread(r); thread third = new thread(r); first.start(); second.start(); third.start(); } }
is correct way??
you looking thread.join()
the join method allows 1 thread wait completion of another. if t thread object thread executing,
t.join();
causes current thread pause execution until t's thread terminates. overloads of join allow programmer specify waiting period. however, sleep, join dependent on os timing, should not assume join wait long specify.
like sleep, join responds interrupt exiting interruptedexception.
so like
first.start(); first.join(); second.start(); second.join(); third.start();
on side note can refer thread join() method in java example
Comments
Post a Comment