Suspend And Resume Thread

class susresThread implements Runnable
{
String n;
Thread thrd;
boolean suspended;
susresThread()
{
thrd=new Thread(this,"Suspend Resume Thread");
suspended=false;
thrd.start();
}

public void run()
{
try
{
for(int i=0;i<10;i++)
{
System.out.println("Thread A: i ="+i);
Thread.sleep(200);
synchronized(this)
{
while(suspended)
{
wait();
}
}
}
}
catch(Exception e)
{

}
}
void susThread()
{
suspended=true;
}
synchronized void resThread()
{
suspended=false;
notify();
}
}

public class SuspendResume {
public static void main(String args[])
{
susresThread ob=new susresThread();
try
{
Thread.sleep(1000);
ob.susThread();
System.out.println("Thread suspended");
Thread.sleep(1000);
ob.resThread();
System.out.println("Thread Resumed");
}
catch(Exception e)
{

}
}

}



No comments:

Post a Comment