Java ReentrantLock create a deadlock situation

Description

Java ReentrantLock create a deadlock situation

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Main {
   Lock firstLock = new ReentrantLock();
   Lock secondLock = new ReentrantLock();

   public static void main(String[] args) {
      new Main().start();
   }//www. j  av a  2 s .co m

   private void start() {
      firstLock.lock();
      Thread secondThread = new Thread(new Runnable() {
         public void run() {
            secondLock.lock();
            firstLock.lock();
         }
      });

      secondThread.start();
      try {
         Thread.sleep(250);
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
      secondLock.lock();

      secondLock.unlock();
      firstLock.unlock();

   }

}



PreviousNext

Related