- Java 并发教程
- 并发 - 首页
- 并发 - 概述
- 并发 - 环境设置
- 并发 - 主要操作
- 线程间通信
- 并发 - 同步
- 并发 - 死锁
- 实用类示例
- 并发 - ThreadLocal
- 并发 - ThreadLocalRandom
- Lock 示例
- 并发 - Lock
- 并发 - ReadWriteLock
- 并发 - Condition
- 原子变量示例
- 并发 - AtomicInteger
- 并发 - AtomicLong
- 并发 - AtomicBoolean
- 并发 - AtomicReference
- 并发 - AtomicIntegerArray
- 并发 - AtomicLongArray
- 并发 - AtomicReferenceArray
- Executor 示例
- 并发 - Executor
- 并发 - ExecutorService
- ScheduledExecutorService
- 线程池示例
- 并发 - newFixedThreadPool
- 并发 - newCachedThreadPool
- newScheduledThreadPool
- newSingleThreadExecutor
- 并发 - ThreadPoolExecutor
- ScheduledThreadPoolExecutor
- 高级示例
- 并发 - Futures 和 Callables
- 并发 - Fork-Join 框架
- 并发集合
- 并发 - BlockingQueue
- 并发 - ConcurrentMap
- ConcurrentNavigableMap
- 并发有用资源
- 并发 - 快速指南
- 并发 - 有用资源
- 并发 - 讨论
Java 并发 - Lock 接口
java.util.concurrent.locks.Lock 接口用作线程同步机制,类似于 synchronized 块。新的锁定机制比 synchronized 块更灵活,并提供了更多选项。Lock 和 synchronized 块之间的主要区别如下:
顺序保证 - synchronized 块不提供任何关于等待线程将获得访问权限的顺序的保证。Lock 接口处理此问题。
无超时 - 如果未授予锁,则 synchronized 块没有超时选项。Lock 接口提供了此选项。
单个方法 - synchronized 块必须完全包含在单个方法中,而 Lock 接口的 lock() 和 unlock() 方法可以在不同的方法中调用。
Lock 方法
以下是 Lock 类中可用的一些重要方法。
| 序号 | 方法和描述 |
|---|---|
| 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() 释放锁。 |
示例
以下 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 类允许线程锁定一个方法,即使它已经拥有另一个方法的锁。