import java.lang.*;
import java.util.concurrent.*;
public class SemaGroupTest_new1 {
      public static boolean stop = false;
      public static Thread t1, t2;
      public static void main(String[] args) {
       Object myobject = new Object();
       final Semaphore s = new  Semaphore(1);
       final Semaphore r = new  Semaphore(0);
       ThreadGroup mythreadgroup = new ThreadGroup("MyGroup");
       t1 = new Thread(mythreadgroup, new Runnable() {
               public void run() {
                   while (!stop) { try {
		       System.out.println("s value = " + s.availablePermits());                           
                                      s.acquire();
                                      r.release();
                                   } catch (InterruptedException e) {
                                   System.out.println("got Interrupt t1");
                                   }
                    }
                }
       });
       t2 = new Thread(mythreadgroup, new Runnable() {
               public void run() {
                   int n = 0;
                  while (!stop){ try{
		       System.out.println("r value = " + r.availablePermits()); 
                                       r.acquire();
                                    } catch (InterruptedException e) {
                                        System.out.println("got Interrupt t2");
                                      }
                                   System.out.println(n++);
                                   t1.interrupt();
                                   s.release();
                            }
                     }
          });
          mythreadgroup.setMaxPriority(t1.MIN_PRIORITY);
          t1.start();
          t2.start();
          synchronized (myobject) {
               try {
                    myobject.wait(10000);
               } catch (InterruptedException e) {
                 }
          }
          stop = true;
          try {
               t1.join();
               t2.join();
          } catch (InterruptedException e) {
          }
          mythreadgroup.destroy();
     }
}
