
import java.lang.*;
import Concurrent.*;

public class SemaGroupTest {
	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);

		ThreadGroup mythreadgroup = new ThreadGroup("MyGroup");

		t1 = new Thread(mythreadgroup, new Runnable() {
			public void run() {
				while (!stop) {
					s.P();
					s.V();
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
					}
				}
			}
		});

		t2 = new Thread(mythreadgroup, new Runnable() {
			public void run() {
				int n = 0;
				while (!stop) {
					s.P();
					System.out.println(n++);
					t1.interrupt();
					s.V();
				}
			}
		});
		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();
	}
}