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.
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.
Press the little gearwheel and you will see all active threads. And on each breakpoint you can set the Suspend Policy.
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.
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? 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'.
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.
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? 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.
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();
}
}
I don't think there is a similar functionality. But you can still check by getting the 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
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With