class ChildThread implements Runnable
{
String name;
Thread t;
boolean suspendFlag;
ChildThread(String threadname)
{
name=threadname;
t=new Thread(this,name);
System.out.println("New Thread : "+t);
suspendFlag=false;
t.start();
}
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
System.out.println(name+" : "+i);
Thread.sleep(200);
synchronized(this)
{
while(suspendFlag)
{
wait();
}
}
}
}
catch(InterruptedException e)
{
System.out.println(name+"interrupted");
}
System.out.println(name+" exiting");
}
void suspendThread()
{
suspendFlag=true;
}
synchronized void resumeThread()
{
suspendFlag=false;
notify();
}
}
class SuspendResume
{
public static void main(String args[])
{
ChildThread t1=new ChildThread("One");
ChildThread t2=new ChildThread("Two");
try
{
Thread.sleep(500);
System.out.println("Suspending thread one");
t1.suspendThread();
Thread.sleep(500);
System.out.println("Resuming thread one");
t1.resumeThread();
System.out.println("Suspending thread two");
t2.suspendThread();
Thread.sleep(500);
System.out.println("Resuming thread two");
t2.resumeThread();
}
catch(InterruptedException e)
{
System.out.println("Main thread interrupted ");
}
try
{
System.out.println("waiting for threads to finish");
t1.t.join();
t2.t.join();
}
catch(InterruptedException e)
{
System.out.println("Main thread interrupted");
}
System.out.println("Main thread exiting ");
}
}
{
String name;
Thread t;
boolean suspendFlag;
ChildThread(String threadname)
{
name=threadname;
t=new Thread(this,name);
System.out.println("New Thread : "+t);
suspendFlag=false;
t.start();
}
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
System.out.println(name+" : "+i);
Thread.sleep(200);
synchronized(this)
{
while(suspendFlag)
{
wait();
}
}
}
}
catch(InterruptedException e)
{
System.out.println(name+"interrupted");
}
System.out.println(name+" exiting");
}
void suspendThread()
{
suspendFlag=true;
}
synchronized void resumeThread()
{
suspendFlag=false;
notify();
}
}
class SuspendResume
{
public static void main(String args[])
{
ChildThread t1=new ChildThread("One");
ChildThread t2=new ChildThread("Two");
try
{
Thread.sleep(500);
System.out.println("Suspending thread one");
t1.suspendThread();
Thread.sleep(500);
System.out.println("Resuming thread one");
t1.resumeThread();
System.out.println("Suspending thread two");
t2.suspendThread();
Thread.sleep(500);
System.out.println("Resuming thread two");
t2.resumeThread();
}
catch(InterruptedException e)
{
System.out.println("Main thread interrupted ");
}
try
{
System.out.println("waiting for threads to finish");
t1.t.join();
t2.t.join();
}
catch(InterruptedException e)
{
System.out.println("Main thread interrupted");
}
System.out.println("Main thread exiting ");
}
}
0 comments:
Post a Comment