Java Concurrency 简明教程
Java Concurrency - Lock Interface
java.util.concurrent.locks.Lock 接口用作与同步块类似的线程同步机制。新的锁定机制更加灵活,并提供比同步块更多的选项。Lock 与同步块之间的主要区别如下 −
-
Guarantee of sequence − 同步块不保证等待线程获得访问权限的顺序。Lock 接口对它进行处理。
-
No timeout − 如果未授予锁,同步块没有超时选项。Lock 接口提供此类选项。
-
Single method − 同步块必须完全包含在一个方法中,而锁接口的方法 lock() 和 unlock() 可以分别位于不同方法中。
Lock Methods
以下是 Lock 类中可用的重要方法列表。
Sr.No. |
Method & Description |
1 |
public void lock() 获得锁。 |
2 |
public void lockInterruptibly() 获取锁,除非当前线程被中断。 |
3 |
public Condition newCondition() 返回一个新的 Condition 实例,该实例绑定到此 Lock 实例。 |
4 |
public boolean tryLock() 仅在调用时锁空闲时才获取锁。 |
5 |
public boolean tryLock(long time, TimeUnit unit) 如果锁在给定的等待时间内是空闲的且当前线程没有被中断,则获取锁。 |
6 |
public void unlock() 释放锁。 |
Example
以下 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();
}
}
这将产生以下结果。
Output
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 类允许一个线程锁定方法,即使它已经在另一个方法上拥有锁。