Deadlocks
The synchronization feature in the Java programming language is convenient and powerful, but it cannot solve all problems that might arise in multithreading. Consider the following situation:
Account 1: $2,000
Account 2: $3,000
Thread 1: Transfer $3,000 from Account 1 to Account 2
Thread 2: Transfer $4,000 from Account 2 to Account 1
As Figure 19 indicates, Threads 1 and 2 are clearly blocked. Neither can proceed because the balances in Accounts 1 and 2 are insufficient.
Figure 19: A deadlock situation
Is it possible that all 10 threads are blocked because each is waiting for more money? Such a situation is called a deadlock.
In our program, a deadlock cannot occur for a simple reason. Each transfer amount is for, at most, $10,000. Since there are 10 accounts and a total of $100,000 in them, at least one of the accounts must have more than $10,000 at any time. The thread moving money out of that account can therefore proceed.
But if you change the run method of the threads to remove the $10,000 transaction limit, deadlocks can occur quickly. Try it out. Construct each TransferThread with a maxAmount of 14000 and run the program. The program will run for a while and then hang.
Another way to create a deadlock is to make the i'th thread responsible for putting money into the i'th account, rather than for taking it out of the i'th account. In this case, there is a chance that all threads will gang up on one account, each trying to remove more money from it than it contains. Try it out. In the SynchBankTest program, turn to the run method of the TransferThread class. In the call to transfer, flip fromAccount and toAccount. Run the program and see how it deadlocks almost immediately.
Here is another situation in which a deadlock can occur easily: Change the notifyAll method to notify in the SynchBankTest program. You will find that the program hangs quickly. Unlike notifyAll, which notifies all threads that are waiting for added funds, the notify method unblocks only one thread. If that thread can't proceed, all threads can be blocked. Consider the following sample scenario of a developing deadlock.
Account 1: $19,000
All other accounts: $9,000 each
Thread 1: Transfer $9,500 from Account 1 to Account 2
All other threads: Transfer $9,100 from their account to another account
Clearly, all threads but Thread 1 are blocked, because there isn't enough money in their accounts.
Thread 1 proceeds. Afterward, we have the following situation:
Account 1: $9,500
Account 2: $18,500
All other accounts: $9,000 each
Then, Thread 1 calls notify. The notify method picks a thread at random to unblock. Suppose it picks Thread 3. That thread is awakened, finds that there isn't enough money in its account, and calls wait again. But Thread 1 is still running. A new random transaction is generated, say,
Thread 1: Transfer $9,600 to from Account 1 to Account 2
Now, Thread 1 also calls wait, and all threads are blocked. The system has deadlocked.
The culprit here is the call to notify. It only unblocks one thread, and it may not pick the thread that is essential to make progress. (In our scenario, Thread 2 must proceed to take money out of Account 2.) In contrast, notifyAll unblocks all threads.
Unfortunately, there is nothing in the Java programming language to avoid or break these deadlocks. You must design your threads to ensure that a deadlock situation cannot occur. You need to analyze your program and ensure that every blocked thread will eventually be notified, and that at least one of them can always proceed.
CAUTION
You should avoid blocking calls inside a synchronized method, for example a call to an I/O operation. If the thread blocks while holding the object lock, every other thread calling a synchronized method on the same object also blocks. If eventually all other threads call a synchronized method on that object, then all threads are blocked and deadlock results. This is called a "black hole." (Think of someone staying in a phone booth for a very long time, and everyone else waiting outside instead of doing useful work.)
Some programmers find Java thread synchronization overly deadlock-prone because they are accustomed to different mechanisms that don't translate well to Java. Simply trying to turn semaphores into a nested mess of synchronized blocks can indeed be a recipe for disaster. Our advice, if you get stuck with a deadlock problem, is to step back and ask yourself what communication pattern between threads you want to achieve. Then create another class for that purpose. That's the Object-Oriented way, and it often helps disentangle the logic of multithreaded programs.
Why the stop and suspend Methods Are Deprecated
The Java 1.0 platform defined a stop method that simply terminates a thread, and a suspend method that blocks a thread until another thread calls resume. Both of these methods have been deprecated in the Java 2 platform. The stop method is inherently unsafe, and experience has shown that the suspend method frequently leads to deadlocks. In this section, you will see why these methods are problematic and what you can do to avoid problems.
Let us turn to the stop method first. When a thread is stopped, it immediately gives up the locks on all objects that it has locked. This can leave objects in an inconsistent state. For example, suppose a TransferThread is stopped in the middle of moving money from one account to another, after the withdrawal and before the deposit. Now the bank object is damaged. That damage is observable from the other threads that have not been stopped.
CAUTION
Technically speaking, the stop method causes the thread to be stopped to throw an exception object of type ThreadDeath. This exception terminates all pending methods, including the run method.
For the same reason, any uncaught exception in a synchronized method can cause that method to terminate prematurely and lead to damaged objects.
When a thread wants to stop another thread, it has no way of knowing when the stop method is safe and when it leads to damaged objects. Therefore, the method has been deprecated.
NOTE
Some authors claim that the stop method has been deprecated because it can cause objects to be permanently locked by a stopped thread. However, that is not true. A stopped thread exits all synchronized methods it has called (through the processing of the ThreadDeath exception). As a consequence, the thread relinquishes the object locks that it holds.
If you need to stop a thread safely, you can have the thread periodically check a variable that indicates whether a stop has been requested.
public class MyThread extends Thread { public void run() { while (!stopRequested && more work to do) { do more work } } public void requestStop() { stopRequested = true; } private boolean stopRequested; }
This code leaves the run method to control when to finish, and it is up to the run method to ensure that no objects are left in a damaged state.
Testing the stopRequested variable in the main loop of a thread work is fine, except if the thread is currently blocked. In that case, the thread will only terminate after it is unblocked. You can force a thread out of a blocked state by interrupting it. Thus, you should define the requestStop method to call interrupt:
public void requestStop() { stopRequested = true; interrupt(); }
You can test the stopRequested variable in the catch clause for the InterruptedException. For example,
try { wait(); } catch (InterruptedException e) { if (stopRequested) return; // exit the run method }
Actually, many programmers take the attitude that the only reason to interrupt a thread is to stop it. Then, you don't need to test the stopRequested variablesimply exit the run method whenever you sense an interrupt.
By the way, the interrupt method does not generate an InterruptedException when a thread is interrupted while it is working. Instead, it simply sets the "interrupted" flag. That way, interrupting a thread cannot corrupt object data. It is up to the thread to check the "interrupted" flag after all critical calculations have been completed.
Next, let us see what is wrong with the suspend method. Unlike stop, suspend won't damage objects. However, if you suspend a thread that owns a lock to an object, then the object is unavailable until the thread is resumed. If the thread that calls the suspend method tries to acquire the lock for the same object before calling resume, then the program deadlocks: the suspended thread waits to be resumed, and the suspending thread waits for the object to be unlocked.
This situation occurs frequently in graphical user interfaces. Suppose we have a graphical simulation of our bank. We have a button labeled "Pause" that suspends the transfer threads, and a button labeled "Resume" that resumes them.
pauseButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { for (int i = 0; i < threads.length; i++) threads[i].suspend(); // Don't do this } }); resumeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { for (int i = 0; i < threads.length; i++) threads[i].resume(); // Don't do this } });
Suppose a paintComponent method paints a chart of each account, calling the bank.getBalance method, and that method is synchronized.
As you will see in the next section, both the button actions and the repainting occur in the same thread, the event dispatch thread.
Now consider the following scenario:
One of the transfer threads acquires the lock on the bank object.
The user clicks the "Pause" button.
All transfer threads are suspended; one of them still holds the lock on the bank object.
For some reason, the account chart needs to be repainted.
The paintComponent method calls the synchronized method bank.getBalance.
Now the program is frozen.
The event dispatch thread can't proceed because the bank object is locked by one of the suspended threads. Thus, the user can't click the "Resume" button, and the threads won't ever resume.
If you want to safely suspend the thread, you should introduce a variable suspendRequested and test it in a safe place of your run methodsomewhere, where your thread doesn't lock objects that other threads need. When your thread finds that the suspendRequested variable has been set, keep waiting until it becomes available again.
For greater clarity, wrap the variable in a class SuspendRequestor, like this:
class SuspendRequestor { public synchronized void set(boolean b) { suspendRequested = b; notifyAll(); } public synchronized void waitForResume() throws InterruptedException { while (suspendRequested) wait(); } private boolean suspendRequested; } class MyThread extends Thread { public void requestSuspend() { suspender.set(true); } public void requestResume() { suspender.set(false); } public void run() { try { while (more work to do) { suspender.waitForResume(); do more work } } catch (InterruptedException exception) { } } private SuspendRequestor suspender = new SuspendRequestor(); }
Now the call to suspender.waitForResume() blocks when suspension has been requested. To unblock, some other thread has to request resumption.
NOTE
Some programmers don't want to come up with a new class for such a simple mechanism. But they still need some object on which to synchronize, because the waiting thread needs to be added to the wait list of some object. It is possible to use a separate dummy variable, like this:
class MyThread extends Thread { public void requestSuspend() { suspendRequested = true; } public void requestResume() { suspendRequested = false; synchronized (dummy) { dummy.notifyAll(); // unblock the thread waiting on dummy } } private void waitForResume() throws InterruptedException { synchronized (dummy) // synchronized necessary for calling wait { while (suspendRequested) dummy.wait(); // block this thread } } . . . private boolean suspendRequested; private Integer dummy = new Integer(1); // any non-null object will work }
We don't like this coding style. It is just as easy to supply an additional class. Then the lock is naturally associated with that class, and you avoid the confusion that arises when you hijack an object just for its lock and wait list.
Of course, avoiding the Thread.suspend method does not automatically avoid deadlocks. If a thread calls wait, it might not wake up either. But there is an essential difference. A thread can control when it calls wait. But the Thread.suspend method can be invoked externally on a thread, at any time, without the thread's consent. The same is true for the Thread.stop method. For that reason, these two methods have been deprecated.
NOTE
In this section, we defined methods requestStop, requestSuspend, and requestResume. These methods provide functionality that is similar to the deprecated stop, suspend, and resume methods, while avoiding the risks of those deprecated methods. You will find that many programmers implement similar methods, but instead of giving them different names, they simply override the stop, suspend, and resume methods. It is entirely legal to override a deprecated method with another method that is not deprecated. If you see a call to stop, suspend, or resume in a program, you should not automatically assume that the program is wrong. First check whether the programmer overrode the deprecated methods with safer versions.
Timeouts
When you make a blocking call, such as a call to the wait method or to I/O, your thread loses control and is at the mercy of another thread or, in the case of I/O, external circumstances. You can limit that risk by using timeouts.
There are two wait methods with timeout parameters:
void wait(long millis) void wait(long millis, int nanos)
that wait until the thread is awakened by a call to notifyAll/notify or for the given number of milliseconds, or milliseconds and nanosecondsthere are 1,000,000 nanoseconds in a millisecond.
However, when the wait method returns, you don't know whether the cause was a timeout or a notification. You probably want to knowthere is no point in reevaluating the condition for which you were waiting if there was no notification.
You can compute the difference of the system time before and after the call:
long before = System.currentTimeMillis(); wait(delay); long after = System.currentTimeMills(); if (after - before > delay) . . . // timeout
Alternatively, you can make the notifying thread set a flag.
A thread can also block indefinitely when calling an I/O operation that doesn't have a timeout. For example, as you will see in Chapter 3, to open a network socket, you need to call the socket constructor, and it doesn't have a provision for a timeout. You can always force a timeout by putting the blocking operation into a second thread, and then use the join method. The call
t.join(millis);
blocks the current thread until the thread t has completed or the given number of milliseconds has elapsed, whichever occurs first. Use this outline:
Thread t = new Thread() { public void run() { blocking operation } }; t.start(); t.join(millis);
The blocking operation either succeeds within the given number of milliseconds, or the join method returns control to the current thread. Of course, then the thread t is still alive. What to do about that depends on the nature of the blocking operation. If you know that the operation can be interrupted, you can call t.interrupt() to stop it. In the case of an I/O operation, you may know that there is an operating-system-dependent timeout. In that case, just leave t alive until the timeout occurs and the blocking operation returns. See the SocketOpener in Chapter 3 for a typical example.
java.lang.Thread
void wait(long millis)
void wait(long millis, int nanos)
causes a thread to wait until it is notified or until the specified amount of time has passed. This method can only be called from within a synchronized method. It throws an IllegalMonitorStateException if the current thread is not the owner of the object's lock.Parameters:
millis
the number of milliseconds
nanos
the number of nanoseconds, < 1,000,000
void join()
waits for the specified thread to cease to be alive.void join(long millis)
waits for the specified thread to cease to be alive or for the specified number of milliseconds to pass.Parameters:
millis
the number of milliseconds