// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.util.concurrent.Semaphore;

public class SemTest {
    public static void main(String[] args) {
        (new ST1()).start();
    }
}

class ST1 extends Thread {
    private Semaphore sem;
    public ST1() {
        this.sem = new Semaphore(1);
    }
    public void run() {
        try {
            sem.acquire();
            (new ST2(sem)).start();
            System.out.println(System.currentTimeMillis());
            Thread.sleep(2000);
            sem.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class ST2 extends Thread {
    private Semaphore sem;
    public ST2(Semaphore sem) {
        this.sem = sem;
    }
    public void run() {
        try {
            sem.acquire();
            System.out.println(System.currentTimeMillis());
            sem.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
