FileDocCategorySizeDatePackage
Deadlock.javaAPI DocExample1634Thu Feb 24 13:26:14 GMT 2000tuning.threads

Deadlock

public class Deadlock extends Object implements Runnable

Fields Summary
String
me
Deadlock
other
Constructors Summary
Methods Summary
public synchronized voidhello()

    //print out hello from this thread then sleep one second.
    System.out.println(me + " says hello");
    try {Thread.sleep(1000);}
    catch (InterruptedException e) {}
  
public voidinit(java.lang.String name, tuning.threads.Deadlock friend)

    //We have a name, and a reference to the other Deadlock object
    //so that we can call each other
    me = name;
    other = friend;
  
public static voidmain(java.lang.String[] args)

    //wait as long as the argument suggests (or use 20 ms as default)
    int wait = args.length == 0 ? 20 : Integer.parseInt(args[0]);

    Deadlock d1 = new Deadlock();
    Deadlock d2 = new Deadlock();

    //make sure the Deadlock objects know each other
    d1.init("d1", d2);
    d2.init("d2", d1);

    Thread d1Thread = new Thread(d1);
    Thread d2Thread = new Thread(d2);

    //Start the first thread, then wait as long as
    //instructed before starting the other
    d1Thread.start();
    try {Thread.sleep(wait);}
    catch (InterruptedException e) {}
    d2Thread.start();
  
public synchronized voidrun()

    //We say we