Java deadlock and solutions

Deadlock is a situation: more than one thread is blocked, one of them or all are waiting for a resource to be released. Since the thread is blocked indefinitely, so the program can not properly terminated.
Four necessary conditions for java Deadlock:
  • 1> exclusive use, that is, when the use of resources is a thread (possession), other threads can not be used
  • 2> can not seize, resource requestor can not be forced to seize resources from the hands of the occupant, resources can only be released by the resource initiative occupants.
  • 3> and holding request, i.e. when the resource requestor holding comrades at the same time the original resource request additional resources.
  • 4> wait loop, i.e. there is a queue: P1 and P2 share of resources, the resources occupied P2 and P3, P1 and P3 share of resources. This forms a waiting loop.
When the above four conditions are fulfilled, it will form a deadlock. Of course, in the case of a deadlock to break if any of these conditions, you can let the deadlock disappear. The following java code used to simulate what a deadlock.
Solution to the deadlock problem is: one is to use synchronized, one is implemented Lock explicit lock.
If an inappropriate use of the lock, and appeared at the same time when you want to lock multiple objects, there will be a deadlock situation, as follows:
import java.util.Date;

public class LockTest {
   public static String obj1 = "obj1";
   public static String obj2 = "obj2";
   public static void main(String[] args) {
      Attract la attract = new ();
      new Thread(la).start();
      LockB lb = new LockB();
      new Thread(lb).start();
   }
}
class LockA implements Runnable{
   public void run() {
      try {
         System.out.println (. New Date () toString () + "LockA started");
         while(true){
            synchronized (LockTest.obj1) {
               System.out.println(new Date().toString() + " LockA 锁住 obj1");
               Thread.sleep (3000); // here is to wait for the opportunity to lock B
               synchronized (LockTest.obj2) {
                  System.out.println(new Date().toString() + " LockA 锁住 obj2");
                  Thread.sleep (60 * 1000); // test, taking up on hold
               }
            }
         }
      } catch (Exception e) {
         e.printStackTrace ();
      }
   }
}
class LockB implements Runnable{
   public void run() {
      try {
         System.out.println (. New Date () toString () + "LockB started");
         while(true){
            synchronized (LockTest.obj2) {
               System.out.println(new Date().toString() + " LockB 锁住 obj2");
               Thread.sleep (3000); // here is to wait for the opportunity to lock A
               synchronized (LockTest.obj1) {
                  System.out.println(new Date().toString() + " LockB 锁住 obj1");
                  Thread.sleep (60 * 1000); // test, taking up on hold
               }
            }
         }
      } catch (Exception e) {
         e.printStackTrace ();
      }
   }
}
operation result: Tue May 05 10:51:06 CST 2015 LockB started Tue May 05 10:51:06 CST 2015 LockA started Tue May 05 10:51:06 CST 2015 LockB lock obj2 Tue May 05 10:51:06 CST 2015 LockA lock obj1

  

Guess you like

Origin www.cnblogs.com/Andrew520/p/12093320.html