Java併發Lock接口

java.util.concurrent.locks.Lock接口用作線程同步機制,類似於同步塊。新的鎖定機制更靈活,提供比同步塊更多的選項。 鎖和同步塊之間的主要區別如下:

  • 序列的保證 - 同步塊不提供對等待線程進行訪問的序列的任何保證,但Lock接口處理它。
  • 無超時,如果未授予鎖,則同步塊沒有超時選項。Lock接口提供了這樣的選項。
  • 單一方法同步塊必須完全包含在單個方法中,而Lock接口的方法lock()unlock()可以以不同的方式調用。

Lock類中的方法

以下是Lock類中可用的重要方法的列表。

編號

方法

描述說明

1

public void lock()

獲得鎖

2

public void lockInterruptibly()

獲取鎖定,除非當前線程中斷

3

public Condition newCondition()

返回綁定到此Lock實例的新Condition實例

4

public boolean tryLock()

只有在調用時纔可以獲得鎖

5

public boolean tryLock(long time, TimeUnit unit)

如果在給定的等待時間內自由,並且當前線程未被中斷,則獲取該鎖。

6

public void unlock()

釋放鎖

示例

以下TestThread程序演示了使用Lock接口的一些方法。 這裏我們使用lock()獲取鎖和unlock()來釋放鎖。

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

class PrintDemo {

   private final Lock queueLock = new ReentrantLock();

   public void print() {
      queueLock.lock();
      try {
         Long duration = (long) (Math.random() * 10000);
         System.out.println(Thread.currentThread().getName() 
            + "  Time Taken " + (duration / 1000) + " seconds.");
         Thread.sleep(duration);
      } catch (InterruptedException e) {
         e.printStackTrace();
      } finally {
         System.out.printf("%s printed the document successfully.\n", Thread.currentThread().getName());
         queueLock.unlock();
      }
   }
}

class ThreadDemo extends Thread {
   PrintDemo  printDemo;

   ThreadDemo( String name,  PrintDemo printDemo) {
      super(name);
      this.printDemo = printDemo;
   }   

   @Override
   public void run() {
      System.out.printf("%s starts printing a document\n", Thread.currentThread().getName());
      printDemo.print();
   }
}

public class TestThread {

   public static void main(String args[]) {
      PrintDemo PD = new PrintDemo();

      ThreadDemo t1 = new ThreadDemo( "Thread - 1 ", PD );
      ThreadDemo t2 = new ThreadDemo( "Thread - 2 ", PD );
      ThreadDemo t3 = new ThreadDemo( "Thread - 3 ", PD );
      ThreadDemo t4 = new ThreadDemo( "Thread - 4 ", PD );

      t1.start();
      t2.start();
      t3.start();
      t4.start();
   }
}

執行上面示例代碼,得到以下結果 -

Thread - 1  starts printing a document
Thread - 4  starts printing a document
Thread - 3  starts printing a document
Thread - 2  starts printing a document
Thread - 1   Time Taken 4 seconds.
Thread - 1  printed the document successfully.
Thread - 4   Time Taken 3 seconds.
Thread - 4  printed the document successfully.
Thread - 3   Time Taken 5 seconds.
Thread - 3  printed the document successfully.
Thread - 2   Time Taken 4 seconds.
Thread - 2  printed the document successfully.

在上面的示例中,使用ReentrantLock類作爲Lock接口的一個實現。 ReentrantLock類允許線程鎖定方法,即使它已經具有其他方法鎖。