在 java.lang.Thread 中有一个方法叫 holdsLock (),它返回 true 如果当且仅当当前线程拥有某个具体对象的锁。
Object o = new Object();
@Test
public void test1() throws Exception {
new Thread(new Runnable(){
@Override
public void run(){
synchronized(o){
System.out.println("child thread: holdlock:"+
Thread.holdsLock(o));
}
}
}).start();
System.out.println("main thread: holdLock:"+ Thread.holdsLock(o));
Thread.sleep(2000);
}
main thread: holdLock: false
child thread: holdLock: true
Was this helpful?
0 / 0