Advertisement
bhushan23

suspend

Jul 13th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1.  
  2. class thread implements Runnable
  3. {
  4. String name;
  5. Thread t;
  6. boolean condition=false;
  7.       thread(String n)
  8.       {
  9.           name=n;
  10.           t=new Thread(this,name);
  11.           t.start();
  12.       }
  13.       public void run()
  14.       {try{
  15.           for(int i=0;i<10;i++)
  16.           {
  17.               System.out.println(name +" :"+i);
  18.               Thread.sleep(200);
  19.               synchronized(this)
  20.               {
  21.                   while(condition)
  22.                       wait();
  23.               }
  24.           }
  25.       }catch(InterruptedException e)
  26.       {
  27.           System.out.println("error in run");
  28.       }
  29.       System.out.println("exiting...");
  30.       }
  31.        void mysuspend()
  32.     {
  33.            //System.out.println(name+":suspending");
  34.           condition=true;
  35.       }
  36.       synchronized void myresume()
  37.       {
  38.           //System.out.println(name+":resuming");
  39.           condition=false;
  40.           notify();
  41.       }
  42.      
  43.      
  44. }
  45. public class suspend {
  46.  
  47.     public static void main(String argv[])
  48.     {
  49.         thread ob1=new thread("one");
  50.         thread ob2=new thread("two");
  51.         try{
  52.             Thread.sleep(1000);
  53.             ob1.mysuspend();
  54.             System.out.println("thread 1 suspened");
  55.             Thread.sleep(1000);
  56.             ob1.myresume();
  57.             System.out.println("thread 1 resuming");
  58.             Thread.sleep(1000);
  59.             ob2.mysuspend();
  60.             System.out.println("thread 2 suspened");
  61.             Thread.sleep(1000);
  62.             ob2.myresume();
  63.             System.out.println("thread 2 resuming");
  64.    
  65.         }catch(InterruptedException e)
  66.         {
  67.             System.out.println("error in main");
  68.         }
  69.         try{
  70.             ob1.t.join();
  71.             ob2.t.join();
  72.         }catch(InterruptedException e)
  73.         {
  74.             System.out.println("error in joining");
  75.         }
  76.        
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement