import java.lang.*;
import java.util.concurrent.*;
public class SemaGroupTest_new6 {
      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("t1: s value = " + s.availablePermits());                           
                                      s.acquire();
				      System.out.println("t1: before releasing r");
                                      r.release();
                                   } catch (InterruptedException e) {
                                   System.out.println("t1: got Interrupt,s value = " +s.availablePermits());
                                   }
                    }
                }
       });
       t2 = new Thread(mythreadgroup, new Runnable() {
               public void run() {
                   int n = 0;
                  while (!stop){ try{
		       System.out.println("t2: r value = " + r.availablePermits()); 
                                       r.acquire();
                                    } catch (InterruptedException e) {
                                        System.out.println("got Interrupt t2");
                                      }
                                   System.out.println(n++);
                                   if ((n%10) ==0 ) t1.interrupt();
				   else 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();
     }
}
