Please send questions to
st10@humboldt.edu .
//--------
// this code is adapted from Flanagan, "Java Examples in a Nutshell",
// O'Reilly, pp. 224-225
//
// it shows how synchronizing threads can lead to deadlock... 8-)
// (this is AWT code)
//
// last modified: 10-20-00
//--------
// "This is a demonstration of how NOT to write multi-threaded programs.
// It is a program that purposely causes deadlock between two threads
// that are both trying to acquire locks for the same two resources."
//
// "To avoid this sort of deadlock when locking multiple resources,
// all threads should always acquire their locks in the same order"
public class Deadlock
{
public static void main(String args[])
{
// "these are the two resource objects we'll try
// to get locks for"
final Object resource1 = "resource1";
final Object resource2 = "resource2";
// "here's the first thread. It tries to lock resource1
// then resource2."
Thread t1 = new Thread()
{
public void run()
{
// "lock resource 1"
System.out.println("Thread 1: about to try to lock resource 1");
synchronized(resource1)
{
System.out.println("Thread 1: locked resource 1");
// "pause for a bit, simulating some file I/O or
// something. Basically, we just want to give
// the other thread a chance to run. Threads and
// deadlocks are asynchronous things, but we're
// trying to force deadlock to happen here..."
try
{
Thread.sleep(50);
}
catch(InterruptedException exc)
{
}
// "now wait 'till we can get a lock on resource 2"
System.out.println("Thread 1: about to try to lock "
+ "resource 2");
synchronized(resource2)
{
System.out.println("Thread 1: locked resource 2");
}
}
System.out.println("Thread 1: done.");
}
};
// "here's the second thread. It tries to lock resource2
// then resource1."
Thread t2 = new Thread()
{
public void run()
{
// "this thread locks resource 2 right away"
System.out.println("Thread 2: about to try to lock resource 2");
synchronized(resource2)
{
System.out.println("Thread 2: locked resource 2");
// "then it pauses, for the same reason as the first
// thread does"
try
{
Thread.sleep(50);
}
catch(InterruptedException exc)
{
}
// "then it tries to lock resource1. But wait!
// Thread 1 locked resource1, and won't release it
// 'till it gets a lock on resource2. This thread
// holds the lock on resource2, and won't release
// it 'till it gets resource1. We're at an impasse.
// Neither thread can run, and the program freezes
// up."
System.out.println("Thread 2: about to try to lock "
+ "resource 1");
synchronized(resource1)
{
System.out.println("Thread 2: locked resource 1");
}
}
System.out.println("Thread 2: done");
}
};
// "start the two threads. If all goes as planned, deadlock will
// occur, and the program will never exit."
t1.start();
t2.start();
}
}