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

is that possible to read before write using ReentrantReadWriteLock?

I am implementing a database which can read and write data. For concurrency issue, I need to implement lock. Normally, ReentrantReadWriteLock will let write execute before read. How Can I go reversely? Is there a way that I can read before write…
Kevin Qu
  • 107
  • 1
  • 1
  • 9
1
vote
3 answers

Java lock and happend-before relation

I'm not sure if I'm interpreting the javadoc right. When using a ReentrantLock after calling the lock method and successfully gaining a lock, can you just access any object without any synchronized blocks and the happend-before relationship is…
Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
1
vote
2 answers

Share RLock between multiple instances of Python with multiprocessing

Consider this MWE: from multiprocessing import RLock class TheSetup: def __init__(self): self._hardware_lock = RLock() def hold_hardware(self): return self._hardware_lock def do_thing_with_hardware(self): …
user171780
  • 2,243
  • 1
  • 20
  • 43
1
vote
0 answers

Remove unused ReentrantLock from ConcurrentHashMap

I have a method in service which fetches entity if exists otherwise it creates new one and returns (for questions purposes let's say entity is a just a string). Method is executed by multiple threads so if two of them will call method…
1
vote
1 answer

What happens if we call condition.await() multiple times by the current thread in Java

I would like to understand what really happens when we execute the following code snippet. When a thread is executes the someMethod() and hits the while loop, what happens if it calls the await method again and again? How is the thread context…
elsa
  • 155
  • 1
  • 2
  • 10
1
vote
2 answers

multiprocessing > Manager() > RLock Error:

I've got a collection of multiprocessing.Process objects in a list, and they all use the same instance of what I will call a "process safe queue" to communicate in a process-safe (thread-safe but with processes) way to the parent process whose…
bitcycle
  • 7,632
  • 16
  • 70
  • 121
1
vote
1 answer

How can I fix this "not quite synchronized" consumer producer example

I am trying to familiarize myself with the ReentrantLock and ConditionVariable classes. I implemented this Scala code ( without anything "Scala specific" in it ): object Conditioned { var pops = 0 var max = 20 abstract…
Senthess
  • 17,020
  • 5
  • 23
  • 28
1
vote
1 answer

What is the equivalent of synchronized( object ) in Reentrant lock in Java?

In the code given below. How can I know which object Reentrant Lock has locked ? Public class Test { Map obj1 = new HashMap(); Object ob2 = new Object(); void Method() { //First synchronized(ob1) { // Locks obj1 …
1
vote
1 answer

Check if lock is held but not lock it if it is free

I have scenario where i have multiple counters object. Each counters object can be incremented by multiple threads at the same time so there is a set of ReentrantLock for all objects and this works good - each object can be modified only by one…
Tomasz Skaba
  • 35
  • 1
  • 5
1
vote
1 answer

SonarIssue: Does not release lock on all paths

I have code something like below mentioned. I am releasing the lock in finally block but still sonarqube is showing "Does not release lock on all paths" message. I tried changing lock.isHeldByCurrentThread() to lock.isLocked() but still no luck. And…
Bhargav V
  • 9
  • 3
1
vote
1 answer

How is unlock happening after await

I wrote a small program to print odd-even numbers alternatively but have a question: Since thread should wait at await call so how is reentrant lock is getting unlocked? public class Worker implements Runnable { private ReentrantLock rLock =…
Azodious
  • 13,752
  • 1
  • 36
  • 71
1
vote
0 answers

ReEntrantLcoks-What is the use of having multiple locks on the same object

While going through the advantages of ReEntrantLock one of them is: With ReentrantLock, a thread can acquire lock on the same object more than once. I came across this answer which states that there would be a deadlock in the get(index). Can…
ghostrider
  • 2,046
  • 3
  • 23
  • 46
1
vote
1 answer

Unexpected results in Java multi threads despite locks

I'm struggling with some exercise code that should calculate sum of squares using threads. For some reason I get inconsistent results despite the use of locks, also made sure I am locking on objects and not local items / variables etc. When I add…
Yaniv
  • 13
  • 3
1
vote
3 answers

Locking all instances of a class in Java

I am implementing a parallel banking system, where all operations can run concurrently. I have implemented a thread safe transferMoney method, that transfers amount from Account from to to. transferMoney is implemented with the following…
pr0f3ss
  • 527
  • 1
  • 4
  • 17
1
vote
1 answer

Why ReentrantLock is not working while synchronized work in the demo?

I am trying to follow the ReentrantLock Example in Java, Difference between synchronized vs ReentrantLock kind of tutorial. And I have a demo started with -ea on as public class ReentrantLockZero { private static ReentrantLock CountLock = new…
Hearen
  • 7,420
  • 4
  • 53
  • 63