本文介绍了Java线程转储:“等待锁定”与“等待锁定”之间的区别和“停车等待”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java线程转储中,您可以看到堆栈跟踪中提到的锁。

似乎有三种信息:

In a Java thread dump, you can see locks mentioned within stack traces.
There seems to be three kinds of information:

1 :

- locked <0x00002aab329f7fa0> (a java.io.BufferedInputStream)

2:

- waiting to lock <0x00002aaaf4ff6fa0> (a org.alfresco.repo.lock.LockServiceImpl)

3:

- parking to wait for  <0x00002aaafbf70bb8> (a java.util.concurrent.SynchronousQueue$TransferStack)




  • 1:该线程已获得对象0x00002aab329f7fa0的锁定。

  • 2& 3:似乎说该线程正在等待所述对象上的锁定变为可用...

    但差价2和3是什么?

  • 推荐答案

    你会得到等待当使用来自java.util.concurrent的锁时,在使用内部锁时锁定在线程转储中和等待停放。请考虑以下示例:

    You will get "waiting to lock" in the thread dump when using intrinsic locks and "parking to wait for" when using locks from java.util.concurrent. Consider the following example:

    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class LockTest {
    
        final Lock lock = new ReentrantLock(true);
    
        synchronized void intrinsicLock() {
            Thread th = new Thread(new Runnable() {
                public void run() {
                    intrinsicLock();
                }
            }, "My thread");
            th.start();
            try {
                th.join();
            } catch (InterruptedException e) {
            }
        }
    
        void reentrantLock() {
            lock.lock();
            Thread th = new Thread(new Runnable() {
                public void run() {
                    reentrantLock();
                }
            }, "My thread");
            th.start();
            try {
                th.join();
            } catch (InterruptedException e) {
            }
            lock.unlock();
        }
    
    
        public static void main(String[] args) {
            LockTest lockTest = new LockTest();
            lockTest.intrinsicLock();
            //lockTest.reentrantLock();
        }
    
    }
    

    lockTest.intrinsicLock()您将获得以下线程转储:

    With lockTest.intrinsicLock() you will get the following thread dump:

    "My thread" prio=10 tid=0x00007fffec015800 nid=0x1775 waiting for monitor entry [0x00007ffff15e5000]
       java.lang.Thread.State: BLOCKED (on object monitor)
        at LockTest.intrinsicLock(LockTest.java:9)
        - waiting to lock <0x00000007d6a33b10> (a LockTest)
        at LockTest$1.run(LockTest.java:11)
        at java.lang.Thread.run(Thread.java:662)
    

    lockTest.reentrantLock()产生:

    "My thread" prio=10 tid=0x00007fffec082800 nid=0x17e8 waiting on condition [0x00007ffff14eb000]
       java.lang.Thread.State: WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x00000007d6a33d30> (a java.util.concurrent.locks.ReentrantLock$FairSync)
        at java.util.concurrent.locks.LockSupport.park(LockSupport.java:156)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:811)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:842)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1178)
        at java.util.concurrent.locks.ReentrantLock$FairSync.lock(ReentrantLock.java:201)
        at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:262)
        at LockTest.reentrantLock(LockTest.java:22)
        at LockTest$2.run(LockTest.java:25)
        at java.lang.Thread.run(Thread.java:662)
    

    这篇关于Java线程转储:“等待锁定”与“等待锁定”之间的区别和“停车等待”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:09