gxh973121

博客园 首页 新随笔 联系 订阅 管理

http://blog.csdn.net/w410589502/article/details/54949506

 

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

posted on 2018-03-12 17:42  gxh973121  阅读(797)  评论(0编辑  收藏  举报