_Java Deadlock_
by Allan Vermeulen

Listing One
public class Resource implements Runnable {
  private Resource boss_;   // a property
  public void setBoss(Resource boss)  {boss_=boss;}
  public Resource getBoss() {return boss_;}

  public synchronized void assignProject() {pause();}
  public synchronized void receiveProject() {}
  public synchronized void run() {
    pause();
    getBoss().assignProject();
  }
  public static void pause() {
    try { Thread.sleep(700); }
    catch(InterruptedException e) { }
  }
}

Listing Two
public class Launch {
  public static void main(String argv[]) {
    Resource boss    = new Resource();
    Resource dilbert = new Resource();
    Resource wally   = new Resource();
    dilbert.setBoss(boss);
    wally.setBoss(boss);

    // Build project threads
    Thread dilbertThread = new Thread(dilbert);
    Thread wallyThread = new Thread(wally);

    // Run the project threads
    dilbertThread.start();
    wallyThread.start();
  }
}

Listing Three
public class Dead1 {
  public static void main(String argv[]) {
    // Create an organization that gets no work done
    Resource dilbert = new Resource();
    Resource wally   = new Resource();
    dilbert.setBoss(wally);
    wally.setBoss(dilbert);

    // Build project threads
    Thread dilbertThread = new Thread(dilbert);
    Thread wallyThread = new Thread(wally);

    // Run the project threads
    dilbertThread.start();
    wallyThread.start();
  }
}

Listing Four
public class Manager extends Resource {
  private boolean hasProject_ = false;
  public synchronized void receiveProject() {
    hasProject_ = true;
    notify();
  }
  public synchronized void assignProject() {
    while (hasProject_ == false) {
      try { wait(); }
      catch(InterruptedException e) { }
    }
    hasProject_ = false;
  }
}

Listing Five
public class Signal {
  public static void main(String argv[]) {
    // Create an organization to get work done
    Resource boss    = new Manager();
    Resource dilbert = new Resource();
    Resource catbert = new Marketer();
    dilbert.setBoss(boss);
    catbert.setBoss(boss);

    // Build the threads in which projects will be done
    Thread dilbertThread = new Thread(dilbert);
    Thread catbertThread = new Thread(catbert);

    // Run the project threads
    dilbertThread.start();
    Resource.pause(); // Dilbert starts first
    catbertThread.start();
  }
}

Listing Six
public class WimpManager extends Resource {
  public synchronized void receiveProject()
    { getBoss().receiveProject(); }
  public synchronized void assignProject()
    { getBoss().assignProject(); }
}

Listing Seven
public class Dead2 {
  public static void main(String argv[]) {
    // Create an organization to get work done
    Resource wimp    = new WimpManager();
    Resource boss =    new Manager();
    Resource dilbert = new Resource();
    Resource catbert = new Marketer();
    wimp.setBoss(boss);
    dilbert.setBoss(wimp);

    // Build the threads in which projects will be done
    Thread dilbertThread = new Thread(dilbert);
    Thread catbertThread = new Thread(catbert);

    // Run the project threads
    dilbertThread.start();
    Resource.pause();  // Dilbert asks first
    catbertThread.start();
  }
}





