Java - calling a synchronous method from internal synchronization while waiting

Is it possible to call a synchronized method from a synchronous method, waiting for the resource to become available (using wait ())? thanks

+4
source share
2 answers

Java mutexes are recursive, so you can call the synchronous method recursively or call another synchronized method for which you already have a lock. You will need to tell us what exactly you are trying to do, though .... a lot of things with thread blocking / synchronization, if done incorrectly, can lead to a deadlock, and it’s not clear from your question what you are trying to do.

+9
source

Not. There is no way to check if a given object monitor is paused in java, or an atomic way to "check and lock" an object lock.

You might want to check out the standard java package http://java.sun.com/javase/6/docs/api/java/util/concurrent/locks/package-summary.html . the Lock class has a tryLock () function that can try to get a lock (therefore, if your thread cannot capture the lock, then it can do something else depending on the return value of tryLock ()).

0
source

Source: https://habr.com/ru/post/1305820/


All Articles