Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if thread holds the monitor in 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.

enter image description here

like image 800
Sachin Sachdeva Avatar asked May 02 '20 09:05

Sachin Sachdeva


People also ask

How do I monitor threads in Intellij?

You can customize how threads are displayed on the Frames and Threads tabs. It might be helpful when you are working on a multithreaded application and often need to access information about your threads. Right-click anywhere in the Frames or Threads tab and select Customize Threads View.

How do I see active threads in Intellij?

Press the little gearwheel and you will see all active threads. And on each breakpoint you can set the Suspend Policy.

How do I debug threads in Intellij?

Start the debug session by clicking the Run button near the main method and selecting Debug. When the program has run, both threads are individually suspended in the addIfAbsent method. Now you can switch between the threads (in the Frames or Threads tab) and control the execution of each thread.

Why debug is not working in Intellij?

To solve this, simply remove the jar of the debugged module from all modules' dependencies in the Project Structure. If you do not know which modules have the debugged module jar as dependencies, you can use some tools (Eg. Sublime Text, bash, ...) to search for the module name which is stored in Intellij *.

Does IntelliJ IDEA provide thread-debugging?

Does IntelliJ IDEA provide thread-debugging? That is - Netbeans allows you to debug multiple threads, and halting at those breakpoints (automatically). However all I seem to be getting in IntelliJ is "thread dumping", which seems to be an analysis that is manual, and a snapshot taken when I clicked 'Thread Dump'.

How do I monitor the contents of the clipboard in IntelliJ IDEA?

Automatically detect and analyze thread dumps copied to the clipboard outside of IntelliJ IDEA. If this checkbox is selected, IntelliJ IDEA will monitor and analyze the contents of the clipboard. You can select this checkbox only once and your clipboard will be scanned every time you switch to IntelliJ IDEA.

Can I customize the display format in IntelliJ IDEA?

The degree to which the display format can be customized has virtually no limitations as IntelliJ IDEA lets you write your own code for rendering data. You can customize how threads are displayed on the Frames and Threads tabs.

How to monitor a thread's status in Java?

How to Monitor a Thread’s Status in Java? The Java language support thread synchronization through the use of monitors. A monitor is associated with a specific data item and functions as a lock on that data. When a thread holds the monitor for some data item, other threads are locked out and cannot inspect or modify the data.


3 Answers

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();
    }
}
like image 107
Foivoschr Avatar answered Oct 21 '22 02:10

Foivoschr


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

Get Thread Dump

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

like image 35
Tarun Lalwani Avatar answered Oct 21 '22 02:10

Tarun Lalwani


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).

enter image description here

like image 2
Mukul Bansal Avatar answered Oct 21 '22 00:10

Mukul Bansal