Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

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 1–9 indicates, Threads 1 and 2 are clearly blocked. Neither can proceed because the balances in Accounts 1 and 2 are insufficient.

Figure 1–9: 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 variable—simply 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:

  1. One of the transfer threads acquires the lock on the bank object.

  2. The user clicks the "Pause" button.

  3. All transfer threads are suspended; one of them still holds the lock on the bank object.

  4. For some reason, the account chart needs to be repainted.

  5. 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 method—somewhere, 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 nanoseconds—there 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 know—there 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

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020