classPrintDemo
{
public void printCount()
{
try
{
for(int i = 5; i > 0; i--)
{
System.out.println("Counter --- " + i );
}
}
catch (Exception e)
{
System.out.println("Thread interrupted.");
}
}
}
classThreadDemo extends Thread
{
private Thread t;
private String threadName;
PrintDemo PD;
ThreadDemo( String name, PrintDemopd)
{
threadName = name;
PD = pd;
}
public void run()
{
synchronized(PD)
{
PD.printCount();
}
System.out.println("Thread " + threadName + " exiting.");
}
public void start ()
{
System.out.println("Starting " + threadName );
if (t == null)
{
t = new Thread (this, threadName);
t.start ();
}
}
}
public class Slip18_1
{
public static void main(String a[])
{
PrintDemo PD = new PrintDemo();
ThreadDemo T1 = new ThreadDemo( "Thread - 1 ", PD );
ThreadDemo T2 = new ThreadDemo( "Thread - 2 ", PD );
T1.start();
T2.start();
// wait for threads to end
try
{
T1.join();
T2.join();
}
catch( Exception e)
{
System.out.println(e);
}
}
}