I am new to multithreading. I know the following things about threads:
thread_obj.join()
holds the thread until it overthread_obj.join(5000)
holds the thread for 5 sec. after then run as other thread.
I know that join()
and join(millis)
that hold thread for some time. My question is that if we provide more time in join()
then sleep()
like join(5000)
and sleep(1000)
it will run fine and thread hold for 5 sec. after then run as other thread but if you provide less time in join()
then sleep()
like, join(500)
and sleep(2000)
the join()
hold thread until it over.
I have following code which I didn't understand join()
public class THRD3 extends Thread {
public void run() {
for(int i = 1 ; i <= 10 ; i++) {
System.out.println(Thread.currentThread().getName()+" : "+i);
try {
//Thread.sleep(500);
Thread.sleep(2000);
}
catch (InterruptedException e) {e.printStackTrace();}
}
}
public static void main(String[] args) throws InterruptedException {
THRD3 obj = new THRD3();
THRD3 obj1 = new THRD3();
THRD3 obj2 = new THRD3();
obj.start();
obj.setName("Joined Thread 0");
//obj.join(2000);
obj.join(500);
obj1.start();
obj2.start();
}
}