ReentrantLock可中断等特性的例子

可中断性

下面这个例子,A先获取锁,然后持有锁睡眠5秒,B尝试获取锁的过程中被中断。于是程序输出

1
2
3
Thread-A acquired the lock.
Thread-B was interrupted while waiting for the lock.
Thread-A released the lock.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class ReentrantLockExample {
private final ReentrantLock lock = new ReentrantLock();

public void doWork() {
try {
// 尝试获取锁,并支持中断
lock.lockInterruptibly();
System.out.println(Thread.currentThread().getName() + " acquired the lock.");

// 模拟一些工作
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " was interrupted while waiting for the lock.");
} finally {
if (lock.isHeldByCurrentThread()) {
lock.unlock();
System.out.println(Thread.currentThread().getName() + " released the lock.");
}
}
}

public static void main(String[] args) {
ReentrantLockExample example = new ReentrantLockExample();

// 创建一个线程 A
Thread threadA = new Thread(() -> example.doWork1(), "Thread-A");
// 创建一个线程 B,会在短暂等待后中断
Thread threadB = new Thread(() -> {
try {
example.doWork1();
} catch (Exception e) {
e.printStackTrace();
}
}, "Thread-B");

threadA.start();
threadB.start();

// 主线程等待 1 秒钟,然后中断 B 线程
try {
Thread.sleep(1000);
threadB.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

如果是用synchronized锁

1
2
3
4
5
6
7
8
9
10
public synchronized void doWork2() {
try {
System.out.println(Thread.currentThread().getName() + " acquired the lock.");
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " was interrupted while waiting for the lock.");
} finally {
System.out.println(Thread.currentThread().getName() + " released the lock.");
}
}

则会输出

1
2
3
4
5
Thread-A acquired the lock.
Thread-A released the lock.
Thread-B acquired the lock.
Thread-B was interrupted while waiting for the lock.
Thread-B released the lock.

超时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public void doWork3() {
try {
// 尝试获取锁,等待最多 2 秒
if (lock.tryLock(2, TimeUnit.SECONDS)) {
System.out.println(Thread.currentThread().getName() + " acquired the lock.");

// 模拟一些工作
Thread.sleep(3000);
} else {
System.out.println(Thread.currentThread().getName() + " could not acquire the lock within timeout.");
}
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " was interrupted.");
} finally {
if (lock.isHeldByCurrentThread()) {
lock.unlock();
System.out.println(Thread.currentThread().getName() + " released the lock.");
}
}
}

public static void testTimeout(ReentrantLockExample example) {
Thread threadA = new Thread(() -> example.doWork3(), "Thread-A");
// 创建一个线程 B,会尝试获取锁但会因为锁被线程A占用而超时
Thread threadB = new Thread(() -> example.doWork3(), "Thread-B");

threadA.start();
threadB.start();
}

会输出

1
2
3
Thread-B acquired the lock.
Thread-A could not acquire the lock within timeout.
Thread-B released the lock.

ReentrantLock可中断等特性的例子
http://hhubibi.github.io/2024/09/05/reentrantlock/
作者
hhubibi
发布于
2024年9月5日
许可协议