Two ReentrantLock : ReentrantLock « JDK 7 « Java






Two ReentrantLock


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

public class Test {
  public static void main(String[] args) {
    final Lock firstLock = new ReentrantLock();
    final Lock secondLock = new ReentrantLock();
    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();
  }
}

 








Related examples in the same category

1.Use ReentrantLock
2.Use ReentrantLock to coordinate
3.LinkedBlockingQueue and ThreadPoolExecutor