My Java application uses two Threads. Historically grown, there are synchronized methods and dedicated lock objects in use. I need to know wether the current thread has a lock and if it is by method or object. How can I do this?
My Java application uses two Threads. Historically grown, there are synchronized methods and dedicated
Share
When entering a synchronized method the VM sets a lock on the current object. Thus the following codes have the same effect:
That means the synchronized method does exactly the same as
anywhere in your code.
You can use
Thread.holdsLock(...)to check if the thread holds a specific lock. Here is an example code:Since Java 1.5 more sophisticated locks like ReentrantReadWriteLock are supported by the
java.util.concurrent.lockspackage.They can provide separated read and write locking and improve the performance of your application. The Lock Objects chapter of the Oracle Java Tutorials is a good start here.