In this tutorial how use is Alive() and join() methods in Multiple Threads in Core Java is shown.
Code:
Thread4.java
class MyThreads extends Thread
{
MyThreads(String name)
{
setName(name);
}
public void run()
{
try
{
for(int i=0;i<10;i++)
{
Thread.sleep(2000);
System.out.print(” “+getName()+” “+String.valueOf(i));
}
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
}
}
class ThreadDemo
{
public static void main(String args[])
{
MyThreads t1 = new MyThreads(“T1”);
MyThreads t2 = new MyThreads(“T2”);
t1.start();
try
{
if(t1.isAlive())
{
t1.join();
}
}
catch(InterruptedException ie)
{
System.out.println(“Thread was interrupted”);
}
t2.start();
}
}