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
2
votes
1 answer

Does a lock do the same for synchronized?

Let's say I have a thread-safe bank account class looks like this class BankAccount { private long balance; public synchronized long getBalance() { return balance; } public synchronized void deposit(long amount) { …
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
2
votes
1 answer

ConcurrentHashMap implementation change from Java 7 to 8

I recently looked at ConcurrentHashMap java 1.8 source code, and there whole implementation is changed from java 1.7. And specifically I am more interested in knowing why for locking purpose they are now using synchronized instead of Reentrantlock…
Vip
  • 1,448
  • 2
  • 17
  • 20
2
votes
2 answers

if ReentrantLock is locked wait but dont lock the lock

I have a ReentrantLock in my code and want to use it to clear an array once per second; I dont want other threads to change the array while it is being cleared, but if I am not currently clearing the array other threads shall not have to wait, like…
Lost2
  • 138
  • 1
  • 13
2
votes
2 answers

Making static ReentrantLocks

I've stumbled upon this thread some time ago: Does making a reentrant lock static and make it a mutex? and I have an additional question on my own: Im interested if creating a private static final ReentrantLock lock is not considered a code smell?…
Bobzone
  • 276
  • 2
  • 12
2
votes
2 answers

State of threads, locks, and conditions

In Java if a thread, t2, attempts to attain a lock, from synchronized, which is currently in use by another thread, t1, then t2 will switch from runnable to blocked. Correct? What about with ReentrantLocks? If the thread t1 finishes using the lock,…
erik p
  • 360
  • 3
  • 15
2
votes
1 answer

What is the difference between ReentrantLock vs stampedlock? Which one to prefer?

What should be the use cases for choosing between ReentrantLock and StampedLock? For example, Which lock should be chosen if I have 10 Readers and 10 Writers? And which one to choose if i have 20 Readers and 1 writers?
KayV
  • 12,987
  • 11
  • 98
  • 148
2
votes
1 answer

Reentrance Lock Condition

public class MyLockConditionTest { private final Lock alock = new ReentrantLock(); private final Condition condition = alock.newCondition(); private String message = null; public void waitForCallback() { alock.lock(); …
2
votes
1 answer

Thread acquires ReentrantLock which is already acquired by other thread

Our application, running in a WebLogic 12c, is retrieving messages from a queuing system, where the queue we are retrieving messages from is configured as a FIFO. We are using Spring to configure the retrieval functionality and both the container…
2
votes
2 answers

Communication between threads in Java without lock

So what i want to achieve is two threads taking turn in executing their task. I originally had just one question; How can i achieve two threads taking turns in executing their task without using a lock? Reason why i don't want the lock is because…
asmb
  • 1,015
  • 2
  • 8
  • 11
2
votes
1 answer

Signal Condition With No Waiting Threads

I'm using a ReentrantLock along with a Condition to synchronise two threads. Whenever the consumer threads performs an await() on the condition before the producer thread performs a signal(), things seem to work fine. However, when the producer…
faridghar
  • 1,445
  • 2
  • 13
  • 25
2
votes
1 answer

Java - How to modify semaphore implementation so it'll be fair

I'm implementing a SimpleSemaphore using ReentrantLock in Java. Now, I would like to add it a fairness flag, to make it behave as a fair\unfair semaphore, as defined in its constructor. Here's my SimpleSemaphore code, I'd be happy for some tips on…
DanielY
  • 1,141
  • 30
  • 58
2
votes
0 answers

Looking for a priorityqueue-like alternative to ReentrantLock for stealing locks from other threads

I have a set of wrapped ReentrantLocks that have unique integer ids, where I require threads to acquire lower-id locks before they acquire higher-id locks in order to prevent deadlock. Three of the locks (lock0, lock1, and lock2) have greater…
Zim-Zam O'Pootertoot
  • 17,888
  • 4
  • 41
  • 69
2
votes
1 answer

ReentrantLock.tryLock(long timeout, TimeUnit unit) does not timeout when can not acquire lock

During integration my project with Ehcache (with BlockingCache decorator, which is internally using ReentrantLock) I found some strange behaviour on one machine. From time to time, threads that are waiting for a lock acquisition via …
szaman
  • 21
  • 1
  • 3
2
votes
2 answers

Reentrant lock - illegalmonitorstateexception when trying to write a byte[] to a serial port using rxtx

I have the following code which is giving me a lot of trouble, I think I've been staring at it too long and fresh eyes would be appreciated - Calling method - (in RobotInterface class) try { …
LiamRyan
  • 1,892
  • 4
  • 32
  • 61
2
votes
1 answer

Is a DelayQueue without fairness problematic?

In Java 7 the implementation of DelayQueue uses a ReentrantLock without a fairness policy. Is this a problem in the long run? Can a thread starve because of this? Thanks
user2027342