public class Mutex
{
  private Thread owner = null;


  public synchronized void lock()
  {
    if(owner == Thread.currentThread()) return;

    while(owner != null) {
      try {
        wait();
      } catch(InterruptedException ex) { }
    }

    owner = Thread.currentThread();
  }

  public synchronized void unlock()
  {
    owner = null;
    notify();
  }
}