关于synchronized 静态方法与实例方法相互调用的问题
2019-01-18 本文已影响4人
王小杰at2019
前言
静态方法锁的是类的描述对象,实例方法锁的当前实例,所以在调用的过程中所有所有的静态同步方法都是同一个锁,而不同实例的同步方法持有的锁是不一样的
例如 ,实例同步方法fun1,静态同步方法fun2 ,线程A在调用fun2 的过程中线程B 是不能调用从fun1中调用fun2是需要等待func2完成的,因为不是同一个锁不适用锁的重入原则
import lombok.extern.slf4j.Slf4j;
import java.lang.management.ManagementFactory;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@Slf4j
public class SynchronizedStaticAndNormalExample {
public synchronized void fun1() {
log.info("lock this fun1");
fun2();
log.info("exit fun1");
}
public synchronized static void fun2() {
log.info("lock class fun2");
try {
TimeUnit.SECONDS.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("exit fun2");
}
public static void main(String[] args) throws InterruptedException {
String name = ManagementFactory.getRuntimeMXBean().getName();
String pid = name.split("@")[0];
log.info("Pid is:" + pid);
log.info("jstack " + pid);
SynchronizedStaticAndNormalExample example = new SynchronizedStaticAndNormalExample();
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.submit(() -> {
fun2();
});
TimeUnit.SECONDS.sleep(5);
executorService.submit(() -> {
example.fun1();
});
executorService.shutdown();
}
}
执行结果
18:32:12.846 [main] INFO cn.wyj.learn.juc.SynchronizedStaticAndNormalExample - Pid is:8976
18:32:12.850 [main] INFO cn.wyj.learn.juc.SynchronizedStaticAndNormalExample - jstack 8976
18:32:12.897 [pool-1-thread-1] INFO cn.wyj.learn.juc.SynchronizedStaticAndNormalExample - lock class fun2
18:32:17.900 [pool-1-thread-2] INFO cn.wyj.learn.juc.SynchronizedStaticAndNormalExample - lock this fun1
18:32:22.898 [pool-1-thread-1] INFO cn.wyj.learn.juc.SynchronizedStaticAndNormalExample - exit fun2
18:32:22.898 [pool-1-thread-2] INFO cn.wyj.learn.juc.SynchronizedStaticAndNormalExample - lock class fun2
18:32:32.898 [pool-1-thread-2] INFO cn.wyj.learn.juc.SynchronizedStaticAndNormalExample - exit fun2
18:32:32.898 [pool-1-thread-2] INFO cn.wyj.learn.juc.SynchronizedStaticAndNormalExample - exit fun1
内存分析结果
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.73-b02 mixed mode):
"DestroyJavaVM" #14 prio=5 os_prio=0 tid=0x00000000029b4000 nid=0x4868 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"pool-1-thread-2" #13 prio=5 os_prio=0 tid=0x000000001f243800 nid=0x1ecc waiting for monitor entry [0x000000001ffaf000]
java.lang.Thread.State: BLOCKED (on object monitor)
at cn.wyj.learn.juc.SynchronizedStaticAndNormalExample.fun2(SynchronizedStaticAndNormalExample.java:20)
- waiting to lock <0x000000076b455aa8> (a java.lang.Class for cn.wyj.learn.juc.SynchronizedStaticAndNormalExample)
at cn.wyj.learn.juc.SynchronizedStaticAndNormalExample.fun1(SynchronizedStaticAndNormalExample.java:15)
- locked <0x000000076bb2bc80> (a cn.wyj.learn.juc.SynchronizedStaticAndNormalExample)
at cn.wyj.learn.juc.SynchronizedStaticAndNormalExample.lambda$main$1(SynchronizedStaticAndNormalExample.java:42)
at cn.wyj.learn.juc.SynchronizedStaticAndNormalExample$$Lambda$2/932583850.run(Unknown Source)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
"pool-1-thread-1" #12 prio=5 os_prio=0 tid=0x000000001f3a2800 nid=0x1a7c waiting on condition [0x000000001fbaf000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:340)
at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
at cn.wyj.learn.juc.SynchronizedStaticAndNormalExample.fun2(SynchronizedStaticAndNormalExample.java:22)
- locked <0x000000076b455aa8> (a java.lang.Class for cn.wyj.learn.juc.SynchronizedStaticAndNormalExample)
at cn.wyj.learn.juc.SynchronizedStaticAndNormalExample.lambda$main$0(SynchronizedStaticAndNormalExample.java:38)
at cn.wyj.learn.juc.SynchronizedStaticAndNormalExample$$Lambda$1/1694819250.run(Unknown Source)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
"Service Thread" #11 daemon prio=9 os_prio=0 tid=0x000000001e84c000 nid=0x2060 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000000001e7b2800 nid=0x32b4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000001e7b0000 nid=0x53a4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000000001e7a7000 nid=0x205c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000000001e7a4000 nid=0x5228 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001e7a1800 nid=0x2d5c runnable [0x000000001ebff000]
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:170)
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 <0x000000076b5174c8> (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 <0x000000076b5174c8> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64)
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001e58c000 nid=0xd44 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001e53d800 nid=0x44f8 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000001c51e000 nid=0x4bb8 in Object.wait() [0x000000001e4fe000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x000000076b107110> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x000000076b107110> (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=0x0000000002aa7000 nid=0x1a14 in Object.wait() [0x000000001e3fe000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x000000076b106b50> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:157)
- locked <0x000000076b106b50> (a java.lang.ref.Reference$Lock)
"VM Thread" os_prio=2 tid=0x000000001c519800 nid=0x3724 runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00000000029ca800 nid=0xc5c runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00000000029cc000 nid=0x3ae8 runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00000000029cd800 nid=0x2294 runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00000000029cf000 nid=0xf0 runnable
"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x00000000029d2800 nid=0x3050 runnable
"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x00000000029d3800 nid=0x1cf4 runnable
"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x00000000029d6800 nid=0x3974 runnable
"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x00000000029d8000 nid=0x1728 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x000000001e8fe000 nid=0x3260 waiting on condition
JNI global references: 338
根据jstack结果我可以看到线程1处于sleeping状态锁定的是0x000000076b455aa8,线程2处于等待锁的状态锁定的是0x000000076bb2bc80, 明显这个两个线程持有的不是同一个锁
"pool-1-thread-2" #13 prio=5 os_prio=0 tid=0x000000001f243800 nid=0x1ecc waiting for monitor entry [0x000000001ffaf000]
java.lang.Thread.State: BLOCKED (on object monitor)
at cn.wyj.learn.juc.SynchronizedStaticAndNormalExample.fun2(SynchronizedStaticAndNormalExample.java:20)
- waiting to lock <0x000000076b455aa8> (a java.lang.Class for cn.wyj.learn.juc.SynchronizedStaticAndNormalExample)
at cn.wyj.learn.juc.SynchronizedStaticAndNormalExample.fun1(SynchronizedStaticAndNormalExample.java:15)
- locked <0x000000076bb2bc80> (a cn.wyj.learn.juc.SynchronizedStaticAndNormalExample)
"pool-1-thread-1" #12 prio=5 os_prio=0 tid=0x000000001f3a2800 nid=0x1a7c waiting on condition [0x000000001fbaf000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:340)
at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
at cn.wyj.learn.juc.SynchronizedStaticAndNormalExample.fun2(SynchronizedStaticAndNormalExample.java:22)
- locked <0x000000076b455aa8> (a java.lang.Class for cn.wyj.learn.juc.SynchronizedStaticAndNormalExample)