Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class thread implements Runnable
- {
- String name;
- Thread t;
- boolean condition=false;
- thread(String n)
- {
- name=n;
- t=new Thread(this,name);
- t.start();
- }
- public void run()
- {try{
- for(int i=0;i<10;i++)
- {
- System.out.println(name +" :"+i);
- Thread.sleep(200);
- synchronized(this)
- {
- while(condition)
- wait();
- }
- }
- }catch(InterruptedException e)
- {
- System.out.println("error in run");
- }
- System.out.println("exiting...");
- }
- void mysuspend()
- {
- //System.out.println(name+":suspending");
- condition=true;
- }
- synchronized void myresume()
- {
- //System.out.println(name+":resuming");
- condition=false;
- notify();
- }
- }
- public class suspend {
- public static void main(String argv[])
- {
- thread ob1=new thread("one");
- thread ob2=new thread("two");
- try{
- Thread.sleep(1000);
- ob1.mysuspend();
- System.out.println("thread 1 suspened");
- Thread.sleep(1000);
- ob1.myresume();
- System.out.println("thread 1 resuming");
- Thread.sleep(1000);
- ob2.mysuspend();
- System.out.println("thread 2 suspened");
- Thread.sleep(1000);
- ob2.myresume();
- System.out.println("thread 2 resuming");
- }catch(InterruptedException e)
- {
- System.out.println("error in main");
- }
- try{
- ob1.t.join();
- ob2.t.join();
- }catch(InterruptedException e)
- {
- System.out.println("error in joining");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement