class MyThread implements Runnable
{
public Thread thrd;
private String thrdname;
boolean suspended;
boolean stopped;
MyThread(String name)
{
thrdname=name;
System.out.println("creating "+thrdname);
}
public void run()
{
System.out.println("Running "+thrdname);
try{
for(int i=1;i<10;i++)
{
System.out.println("Thread:"+thrdname+","+i);
Thread.sleep(50);
synchronized (this)
{
while(suspended)
wait();
}
}
}catch(InterruptedException e)
{
System.out.println(thrd.getName()+" interrupted.");
}
System.out.println(thrd.getName()+" exiting.");
}
public void start()
{
System.out.println("starting"+thrdname);
if(thrd ==null)
{
thrd= new Thread(this,thrdname);
thrd.start();
}
}
synchronized void stop()
{
stopped=true;
suspended=false;
notify();
}
synchronized void suspend()
{
suspended=true;
}
synchronized void resume()
{
suspended=false;
notify();
}
}//thread constructor
public class test
{
public static void main(String args[])throws Exception
{
MyThread mt1 =new MyThread("MyThread");
mt1.start();
MyThread mt2 =new MyThread("MyThread");
mt2.start();
try{
Thread.sleep(100);
mt1.suspend();
System.out.println("Suspending First Thread");
Thread.sleep(100);
mt1.resume();
System.out.println("Resuming First Thread");
mt2.suspend();
System.out.println("Suspending Second Thread");
Thread.sleep(100);
mt2.resume();
System.out.println("resuming Second Thread");
}catch(InterruptedException e)
{
System.out.println("Main thread Interrupted.");
}try{
System.out.println("Waiting for threads to finish.");
mt1.thrd.join();
mt2.thrd.join();
}catch (InterruptedException e){
System.out.println("Main thread Interrupted.");
}
System.out.println("Main thread Exiting.");
}
}
{
public Thread thrd;
private String thrdname;
boolean suspended;
boolean stopped;
MyThread(String name)
{
thrdname=name;
System.out.println("creating "+thrdname);
}
public void run()
{
System.out.println("Running "+thrdname);
try{
for(int i=1;i<10;i++)
{
System.out.println("Thread:"+thrdname+","+i);
Thread.sleep(50);
synchronized (this)
{
while(suspended)
wait();
}
}
}catch(InterruptedException e)
{
System.out.println(thrd.getName()+" interrupted.");
}
System.out.println(thrd.getName()+" exiting.");
}
public void start()
{
System.out.println("starting"+thrdname);
if(thrd ==null)
{
thrd= new Thread(this,thrdname);
thrd.start();
}
}
synchronized void stop()
{
stopped=true;
suspended=false;
notify();
}
synchronized void suspend()
{
suspended=true;
}
synchronized void resume()
{
suspended=false;
notify();
}
}//thread constructor
public class test
{
public static void main(String args[])throws Exception
{
MyThread mt1 =new MyThread("MyThread");
mt1.start();
MyThread mt2 =new MyThread("MyThread");
mt2.start();
try{
Thread.sleep(100);
mt1.suspend();
System.out.println("Suspending First Thread");
Thread.sleep(100);
mt1.resume();
System.out.println("Resuming First Thread");
mt2.suspend();
System.out.println("Suspending Second Thread");
Thread.sleep(100);
mt2.resume();
System.out.println("resuming Second Thread");
}catch(InterruptedException e)
{
System.out.println("Main thread Interrupted.");
}try{
System.out.println("Waiting for threads to finish.");
mt1.thrd.join();
mt2.thrd.join();
}catch (InterruptedException e){
System.out.println("Main thread Interrupted.");
}
System.out.println("Main thread Exiting.");
}
}
SAMPLE OUTPUT:
No comments:
Post a Comment