- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有以下线程转储,它显示两个线程都锁定在同一个对象上。我对它的真正含义感到困惑
"pool-1-thread-2" prio=10 tid=0x00007fd6dc106000 nid=0x5d15 in Object.wait() [0x00007fd6d2067000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007c3547770> (a java.lang.Object)
at java.lang.Object.wait(Object.java:503)
at test.TestDead$Foo.second(TestDead.java:22)
at test.TestDead$Foo.first(TestDead.java:14)
- locked <0x00000007c3547770> (a java.lang.Object)
at test.TestDead$2.run(TestDead.java:45)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
Locked ownable synchronizers:
- <0x00000007c35519e8> (a java.util.concurrent.ThreadPoolExecutor$Worker)
"pool-1-thread-1" prio=10 tid=0x00007fd6dc104800 nid=0x5d14 in Object.wait() [0x00007fd6d2168000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007c3547770> (a java.lang.Object)
at java.lang.Object.wait(Object.java:503)
at test.TestDead$Foo.second(TestDead.java:22)
at test.TestDead$Foo.first(TestDead.java:14)
- locked <0x00000007c3547770> (a java.lang.Object)
at test.TestDead$1.run(TestDead.java:37)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
Locked ownable synchronizers:
- <0x00000007c3551730> (a java.util.concurrent.ThreadPoolExecutor$Worker)
“锁定”在这里的真正含义是什么?
最佳答案
在此上下文中,已锁定 表示您正在运行的 java 代码已进入 synchronous
block 但尚未退出该 block 。
如您的线程转储所示,您正在调用 wait()
在内部 解锁与 synchronous
关联的监视器堵塞。但是,由于您在等待时阻塞并且尚未退出同步块(synchronized block),因此线程转储仍显示 locked
.因此可以有多个线程显示 locked
在线程转储中,尽管底层监视器已解锁。
这可以通过简单的测试轻松证明:
public class TestMonitor {
synchronized public void lockAndWait() {
try {
wait();
} catch ( InterruptedException ex ) {
// Stifle
}
}
public static void main( String args[] ) {
TestMonitor tm = new TestMonitor();
tm.lockAndWait();
}
}
运行时输出以下线程转储:
"main" prio=10 tid=0x00007f86c4008000 nid=0x5d35 in Object.wait() [0x00007f86cbae2000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000759055df8> (a TestMonitor)
at java.lang.Object.wait(Object.java:503)
at TestMonitor.lockAndWait(TestMonitor.java:5)
- locked <0x0000000759055df8> (a TestMonitor)
at TestMonitor.main(TestMonitor.java:13
注意监视器仍然是locked
尽管在 wait
.
如果单线程的情况不令人信服,您可以运行上面的示例稍作修改,在这种情况下您会看到多个线程 locked
在线程转储中的同一监视器上:
public static void main( String args[] ) {
final TestMonitor tm = new TestMonitor();
Thread thread1 = new Thread( new Runnable() { public void run() { tm.lockAndWait(); } } );
Thread thread2 = new Thread( new Runnable() { public void run() { tm.lockAndWait(); } } );
thread1.start();
thread2.start();
}
关于java - 多个线程如何在同一对象上拥有 "locked"(如线程转储所示),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30111864/
我是一名优秀的程序员,十分优秀!