Advertisement
bhushan23

Suspend2

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