如何在IntelliJ中检查线程是否持有监视器?

huangapple go评论67阅读模式
英文:

How to check if thread holds the monitor in IntelliJ?

问题

在使用 IntelliJ 进行工作时,我无法检查线程是否持有锁。

在 Eclipse 的图形界面中,针对线程有一个类似于 lock 的图标,告诉我们它正在持有那个 lock

在下面的代码快照中,我的线程正在执行 notifyElementAdded() 并持有锁,然而在线程堆栈中,IntelliJ 没有显示类似的图标或提示。

所以我的问题是如何在 IntelliJ 图形界面中进行相同的检查。

如何在IntelliJ中检查线程是否持有监视器?

英文:

While working on IntelliJ , I am unable to check that if the thread is holding the lock or not.

On eclipse GUI there is a lock like icon against the thread , telling us that it is holding that lock.

In below code snapshot, my thread is at notifyElementAdded() and holding the lock however, in a thread stack there is no such Icon or intimation from Intellij

So my question is how to check the same on IntelliJ GUI.

如何在IntelliJ中检查线程是否持有监视器?

答案1

得分: 4

Thread 类在 Java 中实际上有一个布尔属性 - Thread.holdsLock()
要获取持有监视器的线程的名称,您可以使用下面的代码示例:

public static long getMonitorOwner(Object obj)
{
    if (Thread.holdsLock(obj)) 
    {
        return Thread.currentThread().getId();
    }
}
英文:

There is actually a boolean attribute to the Thread class in Java - Thread.holdsLock().
To get the name of the thread which holds the monitor you can use the code example below:

public static long getMonitorOwner(Object obj)
{
    if (Thread.holdsLock(obj)) 
    {
        return Thread.currentThread().getId();
    }
}

答案2

得分: 2

我不认为有类似的功能。但你仍然可以通过获取转储信息来检查。

你可以在调试窗口中点击 获取线程转储,然后你可以在日志中看到 locked,以查看线程实际上是否持有锁。

英文:

I don't think there is a similar functionality. But you can still check by getting the dump

如何在IntelliJ中检查线程是否持有监视器?

You can click on Get Thread Dump in Debug window and then you can see the locked in the log to see that the thread is actually holding the lock

答案3

得分: 2

在IntelliJ的调试控制台中,按照下面图像中所示的方法,通过加号按钮创建一个自定义变量。

现在,每当你在调试模式下运行代码时,这个变量将会在所有的调试断点处重新计算。

我创建了一个名为Thread.holdsLock(AwsS3ClientHelper.class)的变量,因为我正在对该类本身进行锁定。你可以在那里写入任何你选择的变量。在你的特定情况下,它将是Thread.holdsLock(observers)

英文:

Create a custom variable in the Intellij debugging console using the plus button as shown in the image below.

Now every time you run the code in the debug mode, this variable will be re-calculated at your all debug points.

I created a variable- Thread.holdsLock(AwsS3ClientHelper.class) since I was acquiring a lock on the class itself. You can write any variable of your choice there. In your particular case, it will be Thread.holdsLock(observers).

如何在IntelliJ中检查线程是否持有监视器?

答案4

得分: 0

这可以作为 IntelliJ 的潜在功能请求,将其包含在他们的 GUI 产品中。

在编程上,要验证这一点,您可以使用 java.lang.Thread.holdsLock() 方法,该方法返回 true 当且仅当当前线程在指定的对象上持有监视器锁。

public static boolean holdsLock(Object obj)

以下是参考的 run 方法代码段,

public void run() {

   /* 如果线程持有监视器锁则返回 true */

   // 返回 false
   System.out.println("Holds Lock = " + Thread.holdsLock(this));  

   synchronized (this) {
      // 返回 true
      System.out.println("Holds Lock = " + Thread.holdsLock(this));
   }
}
英文:

This can be a potential feature request for IntelliJ to include this to their GUI product.

Programmatically, to verify this you can use the java.lang.Thread.holdsLock() method which returns true if and only if the current thread holds the monitor lock on the specified object

public static boolean holdsLock(Object obj)

Below snippet of run method for reference,

public void run() {

   /* returns true if thread holds monitor lock */

   // returns false
   System.out.println("Holds Lock = " + Thread.holdsLock(this));  

   synchronized (this) {
      // returns true
      System.out.println("Holds Lock = " + Thread.holdsLock(this));
   }
 }

huangapple
  • 本文由 发表于 2020年5月2日 17:58:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/61557491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定