gpt4 book ai didi

java - java中如何查找当前拥有锁的线程

转载 作者:搜寻专家 更新时间:2023-10-31 19:49:48 25 4
gpt4 key购买 nike

我在单例类中有一个同步方法,它被许多线程同时调用。是否有任何 Java API 可用于检查当前拥有锁的线程?

最佳答案

erickson 给出的精确答案 [here]

Question: Programmatically determine which Javathread holds a lock ?


Answer :

You can only tell whether the currentthread holds a normal lock(Thread.holdsLock(Object)). You can'tget a reference to the thread that hasthe lock without native code.

However, if you're doing anythingcomplicated with threading, youprobably want to familiarize yourselfwith the java.util.concurrentpackages. The ReentrantLock does allowyou to get its owner (but its aprotected method, so you'd have toextend this). Depending on yourapplication, it may well be that byusing the concurrency packages, you'llfind that you don't need to get thelock's owner after all.

There are non-programmatic methods tofind the lock owners, such assignaling the JVM to issue a threaddump to stderr, that are useful todetermine the cause of deadlocks.

顺便说一句,请看一下下面的链接。它提供了有关线程相关方面的所有信息:

关于java - java中如何查找当前拥有锁的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5090104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com