Questions tagged [reentrantlock]

ReentrantLock is a Java mutex class.

A 'reentrant lock' is a lock that can be re-acquired by the current owner without blocking or deadlock. Java provides an implementation.

230 questions
379
votes
8 answers

Why use a ReentrantLock if one can use synchronized(this)?

I'm trying to understand what makes the lock in concurrency so important if one can use synchronized (this). In the dummy code below, I can do either: synchronized the entire method or synchronize the vulnerable area (synchronized(this){...}) OR…
adhg
  • 10,437
  • 12
  • 58
  • 94
23
votes
5 answers

Unlocking lock owned by another thread java

I have a LockManager that manages the locks of several threads. Sometimes the threads are bad boys, and I have to kill them and ask the LockManager to release all their locks. However, since I use ReentrantLock in java this is impossible, I can not…
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
18
votes
5 answers

Actual use of lockInterruptibly for a ReentrantLock

What do you actually use for this method lockInterruptibly? I have read the API however it's not very clear to me. Could anybody express it in other words?
Rollerball
  • 12,618
  • 23
  • 92
  • 161
16
votes
2 answers

Spring Web Flow LockTimeoutException

We are using Spring Web Flow (2.0.9) in the Weblogic 10 clustured environment. And in production we are getting a lot of LockTimeoutException : Unable to acquire conversation lock after 30 seconds. I have been trying to figure out why does above…
Kunal Jha
  • 2,102
  • 4
  • 24
  • 34
13
votes
2 answers

Does making a Reentrant Lock static and make it a mutex?

In Brian Goetz's book, Java Concurrency in Practice, his example of a Reentrant lock is programmed like this: Lock lock = new ReentrantLock(); However, I am curious to know if changing the above code to: private static final Lock lock = new…
Swifty McSwifterton
  • 2,637
  • 1
  • 30
  • 37
10
votes
1 answer

what's the difference between Lock and ReentrantLock in Java 5?

I don't understand the difference between them. I thought a lock from the lock interface was reentrant too then what's the difference between them? When would you use each?
10
votes
1 answer

Why isn't ReadWriteLock upgrade allowed?

ReadWriteLock downgrade is allowed by ReentrantReadWriteLock implementation (tryLock() from the example below always returns true): void downgrade(final ReadWriteLock readWriteLock) { boolean downgraded = false; …
Bass
  • 4,977
  • 2
  • 36
  • 82
9
votes
4 answers

java.concurrent.ReentrantLock - why we want to acquire the same lock multiple times

I know if using ReentrantLock, it allows the same thread to acquire the same lock more than once. Internally, it has a counter to count the number of the lock acquisition. If you acquired the same lock twice, you would need to release it twice. But…
Shengjie
  • 12,336
  • 29
  • 98
  • 139
8
votes
3 answers

Understanding lock scope

From this link, I understand "Since the lock() and unlock() method calls are explicit, we can move them anywhere, establishing any lock scope, from a single line of code to a scope that spans multiple methods" So what I understand from the above…
S Kr
  • 1,831
  • 2
  • 25
  • 50
8
votes
1 answer

How to implement a reentrant locking mechanism in objective-c through GCD?

I have an objective-c class with some methods, which use a GCD queue to ensure that concurrent accesses to a resource take place serially (standard way to do this). Some of these methods need to call other methods of the same class. So the locking…
Daniel S.
  • 6,458
  • 4
  • 35
  • 78
7
votes
2 answers

Does synchronized block have max reentrant limit?

As we know, ReentrantLock has a max reentrant limit: Integer.MAX_VALUE; Does synchronized block have reentrant limit too? Update: I found it is hard to write test code for synchronized reentrant: public class SyncReentry { public static void…
Jason
  • 521
  • 2
  • 7
7
votes
3 answers

Java lock and unlock on different thread

I have a main thread and a worker thread. The main thread adds tasks into a queue and the worker thread takes them to compute data. Before I put the objects into the queue I call lock on a ReentrantLock object (on the main thread) inside the task…
stonar96
  • 1,359
  • 2
  • 11
  • 39
7
votes
0 answers

IllegalMonitorStateException while unlocking ReentrantLock on Android

I'm using ReentrantLock with its recommended practise (lock, then actual code in try-block, then unlock in finally, see code example below). Sometimes (very very rare) I'm having java.lang.IllegalMonitorStateException exception during unlock. Why it…
nightuser
  • 664
  • 4
  • 13
7
votes
3 answers

can anyone explain how to use Reentrant Lock in java over Synchronized with some best examples

When I run the example class at http://javarevisited.blogspot.in/2013/03/reentrantlock-example-in-java-synchronized-difference-vs-lock.html, I'm seeing the same behavior as with synchronized.
user2473958
  • 79
  • 1
  • 4
7
votes
2 answers

Java : ReentrantReadWriteLock with priority

The following is the typical reader and writer pattern (a lot of reads and few writes) private ReadWriteLock lock = new ReentrantReadWriteLock(); private int value; public void writeValue(int newValue){ lock.writeLock().lock(); …
1
2 3
15 16