📝个人主页:五敷有你
🔥系列专栏:并发编程
⛺️稳重求进,晒太阳
多把锁
多个工作用同一把锁,并发度是很低的
示例
如下代码,在一个大房子里,不同工作共用一把锁(睡觉,学习...)导致并发度很低。
class BigRoom {
public void sleep() {
synchronized (this) {
log.debug("sleeping 2 小时");
Sleeper.sleep(2);
}
}
public void study() {
synchronized (this) {
log.debug("study 1 小时");
Sleeper.sleep(1);
}
}
}
执行
BigRoom bigRoom = new BigRoom();
new Thread(() -> {
bigRoom.compute();
},"小南").start();
new Thread(() -> {
bigRoom.sleep();
},"小女").start();
某次结果
改进
不同的任务用不同的锁
class BigRoom {
private final Object studyRoom = new Object();
private final Object bedRoom = new Object();
public void sleep() {
synchronized (bedRoom) {
log.debug("sleeping 2 小时");
Sleeper.sleep(2);
}
}
public void study() {
synchronized (studyRoom) {
log.debug("study 1 小时");
Sleeper.sleep(1);
}
}
}
某次执行结果
将锁的粒度细分:
好处:是可以增强并发度
坏处:如果一个线程需要同时获得多把锁,就容易发生死锁
活跃性
死锁
有这样的一种情况:一个线程需要同时获取多把锁,这时就容器发生死锁。
t1线程获得A对象锁,接下来想获得B对象的锁。
t2线程获得B对象锁,接下来想获得A对象的锁。
Object A=new Object();
Object B=new Object();
Thread t1=new Thread(()->{
synchronized (A){
log.debug("lock A");
sleep(1);
synchronized (B){
log.debug("lock B");
log.debug("操作...");
}
}
},"t1");
Thread t2=new Thread(()->{
synchronized (B){
log.debug("lock B");
sleep(0.5);
synchronized (A){
log.debug("lock A");
log.debug("操作...");
}
}
},"t2");
t1.start();
t2.start();
运行结果
定位死锁的工具
1.检测死锁可以使用jconsole工具,WIN+R窗口输入jconsole
2.使用jps定位进程id,再用jstack定位死锁
$ jps
26400 RemoteMavenServer36
18708 Launcher
19668 Jps
14568
10124 Test
$ jstack 10124
2024-01-18 10:42:01
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.121-b13 mixed mode):
"DestroyJavaVM" #22 prio=5 os_prio=0 tid=0x0000000002d84000 nid=0x3dac waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"t2" #21 prio=5 os_prio=0 tid=0x00000000215c2000 nid=0x6acc waiting for monitor entry [0x0000000021d2f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at aaa.Test.lambda$main$1(Test.java:36)
- waiting to lock <0x000000076c12dc10> (a java.lang.Object)
- locked <0x000000076c12dc20> (a java.lang.Object)
at aaa.Test$$Lambda$2/1031980531.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"t1" #20 prio=5 os_prio=0 tid=0x00000000215c1800 nid=0x64c0 waiting for monitor entry [0x0000000021c2f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at aaa.Test.lambda$main$0(Test.java:22)
- waiting to lock <0x000000076c12dc20> (a java.lang.Object)
- locked <0x000000076c12dc10> (a java.lang.Object)
at aaa.Test$$Lambda$1/1793329556.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"Service Thread" #19 daemon prio=9 os_prio=0 tid=0x000000001eadb000 nid=0x6a64 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread11" #18 daemon prio=9 os_prio=2 tid=0x000000001ea7f000 nid=0x2338 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread10" #17 daemon prio=9 os_prio=2 tid=0x000000001ea7e800 nid=0x44c4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread9" #16 daemon prio=9 os_prio=2 tid=0x000000001ea80800 nid=0x3168 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread8" #15 daemon prio=9 os_prio=2 tid=0x000000001ea7c000 nid=0x660c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread7" #14 daemon prio=9 os_prio=2 tid=0x000000001ea57000 nid=0x208c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread6" #13 daemon prio=9 os_prio=2 tid=0x000000001ea38800 nid=0x1d9c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread5" #12 daemon prio=9 os_prio=2 tid=0x000000001ea0c800 nid=0x54d0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread4" #11 daemon prio=9 os_prio=2 tid=0x000000001ea04000 nid=0x5be0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000000001e9fb000 nid=0x5938 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000001e9f2800 nid=0x26d8 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000000001e9ef800 nid=0x27f0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000000001e9eb000 nid=0x6bb4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001e9a7000 nid=0x2078 runnable [0x000000002042e000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
- locked <0x000000076b84a128> (a java.io.InputStreamReader)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
- locked <0x000000076b84a128> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:56)
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001e7fb000 nid=0x5b64 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001e858000 nid=0x473c runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000001cd78800 nid=0x55a8 in Object.wait() [0x000000002012e000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x000000076b588ec8> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x000000076b588ec8> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x000000001cd70000 nid=0x4d34 in Object.wait() [0x000000002002e000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x000000076b586b68> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x000000076b586b68> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"VM Thread" os_prio=2 tid=0x000000001e7b5000 nid=0x4554 runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000002d99800 nid=0x27bc runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000002d9b000 nid=0x2c3c runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000002d9c800 nid=0x5770 runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000002d9e000 nid=0x3f2c runnable
"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x0000000002da0000 nid=0x2284 runnable
"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x0000000002da2800 nid=0xe4c runnable
"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x0000000002da5800 nid=0x5914 runnable
"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x0000000002da6800 nid=0x1044 runnable
"GC task thread#8 (ParallelGC)" os_prio=0 tid=0x0000000002da8000 nid=0x4c6c runnable
"GC task thread#9 (ParallelGC)" os_prio=0 tid=0x0000000002da9000 nid=0x467c runnable
"GC task thread#10 (ParallelGC)" os_prio=0 tid=0x0000000002daa000 nid=0x4c60 runnable
"GC task thread#11 (ParallelGC)" os_prio=0 tid=0x0000000002dad800 nid=0x4aac runnable
"GC task thread#12 (ParallelGC)" os_prio=0 tid=0x0000000002dae800 nid=0x57dc runnable
"VM Periodic Task Thread" os_prio=2 tid=0x0000000021320800 nid=0x65e8 waiting on condition
JNI global references: 343
Found one Java-level deadlock:
=============================
"t2":
waiting to lock monitor 0x000000001cd74b18 (object 0x000000076c12dc10, a java.lang.Object),
which is held by "t1"
"t1":
waiting to lock monitor 0x000000001cd76f88 (object 0x000000076c12dc20, a java.lang.Object),
which is held by "t2"
Java stack information for the threads listed above:
===================================================
"t2":
at aaa.Test.lambda$main$1(Test.java:36)
- waiting to lock <0x000000076c12dc10> (a java.lang.Object)
- locked <0x000000076c12dc20> (a java.lang.Object)
at aaa.Test$$Lambda$2/1031980531.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"t1":
at aaa.Test.lambda$main$0(Test.java:22)
- waiting to lock <0x000000076c12dc20> (a java.lang.Object)
- locked <0x000000076c12dc10> (a java.lang.Object)
at aaa.Test$$Lambda$1/1793329556.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Found 1 deadlock.
避免死锁要注意加锁顺序
另外如果由于某个线程进入了死循环,导致其他线程一直等待。对于这种情况Linux下可以通过top先定位CPU占用高的java进程,再利用top -Hp 进程id来定位是哪个线程,最后再用jstack排查