1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| class sus_res_stop implements Runnable()
{
Thread Th;
boolean suspend_flag;
boolean stop_flag;
}
sus_res_stop(String tN)
{
Thread Th = new Thread(this,tN);
boolean suspend_flag = false;
boolean stop_flag = false;
Th.start();
}
public void run()
{
try
{
int j=1;
while(++j<20)
{
synchronized(this)
{
while(suspend_flag)
{wait();}
if (stop_flag)
{ break;}
}
}
}
catch(InterruptedException IE)
{
System.out.println("Thread interrupted");
}
}
synchronized void my_suspend( )
{
suspend_flag=true;
}
synchronized void my_resume( )
{
suspend_flag=false;
notify();
}
synchronized void my_stop( )
{
suspend_flag=false;
stop_flag=true;
notify();
}
}
public class eg_SRS
{
public static void main(String[]args)
{
try
{
sus_res_stop S_R_S_T=new sus_res_stop("SRS");
System.out.println("Thread S_R_S_T is created and started");
Thread.sleep(2000);
S_R_S_T.my_suspend();
System.out.println("Thread S_R_S_T is suspended");
Thread.sleep(2000);
S_R_S_T.my_resume();
System.out.println("Thread S_R_S_T is resumed");
Thread.sleep(2000);
S_R_S_T.my_suspend();
System.out.println("Thread S_R_S_T is suspended");
Thread.sleep(2000);
S_R_S_T.my_resume();
System.out.println("Thread S_R_S_T is resumed");
Thread.sleep(2000);
S_R_S_T.my_stop();
System.out.println("Thread S_R_S_T is stopped");
}
catch(InterruptedException IE)
{
System.out.println("Generated interrupted exception");
}
}
} |