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
1
vote
1 answer

ReentrantLock - unlock() method doesn't seem to work with lists

I'm writing a program that has to modify a List in two ways. Although this implementation works perfectly, it fails to let the second thread acquire the lock: Node head = new Node(new Object(), null); public static ReentrantLock lock = new…
1
vote
2 answers

Why do we need a lock to be reentrant?

I understand (somewhat) the features of the jdk 5 ReentrantLock here But why we would want a 're-entrant' lock? i.e if a Thread already has the lock on an Object, why would it need to acquire it again?
Victor
  • 16,609
  • 71
  • 229
  • 409
1
vote
1 answer

Does fairness guarantee FIFO scheduling?

Directly from this website: But if we are specifying the fairness parameter as “true” while creating a new ReentrantLock object, it gives us the guaranty that the longest waiting thread will get the lock next. Sounds pretty nice right? I…
Rollerball
  • 12,618
  • 23
  • 92
  • 161
1
vote
1 answer

High-Level Concurrency in an Android Game Loop

I am trying to synchronize a pair of non-UI threads, one thread to run game logic and one thread to render, in order to execute tasks in a logical and efficient order. A constraint I imposed myself was that the entire system operate at an…
1
vote
1 answer

Why is there no synchronized keyword used in Java lock implementations?

synchronized is used in Java to deal with mutex sort of things. However the implementations of Lock interface like ReentrantLock in Java does not use this keyword. All the code looks just normal code. Then how does it handle multiple threads on…
NSF
  • 2,499
  • 6
  • 31
  • 55
1
vote
2 answers

How to properly scope a lock

Let's say I have a ReentrantLock, ReentrantLock foo = new ReentrantLock(); and a method bar that uses the lock, public void bar() { foo.lock(); try { methodOne(); } finally { foo.unlock(); } } and…
mre
  • 43,520
  • 33
  • 120
  • 170
1
vote
3 answers

Differences between synchronized keyword and ReentrantLock

I made a thread pool based on the example on this page. In the worker thread we have the infinite loop that never lets the thread die and the wait() method call that pauses the thread when there is no work to do: while (true) { …
Rox
  • 2,647
  • 15
  • 50
  • 85
0
votes
2 answers

Java ReentrantLock by ID

I'm working on Java and I have a "batch" procedure, with multiple objects, and each one of them does something on the db (mainly updates). I want some of them to wait each other, but only if a specific condition is encountered. To be precise, some…
Cliser
  • 3
  • 1
0
votes
0 answers

Java Queue with Consumer and Producer (ReentrantLock) Issue

There is a queue with priorities and four threads – two produce and enter priority values into the queue (the value and priority are the same) and two extract priority values from it. And I need to separate Consumer Threads with this logic: The…
0
votes
0 answers

Is there any chance that even though the lock object is free thread fails to acquire the lock?

I'm working on multithreading issue where threads failed to acquire the lock even though the lock is free i.e., none of the threads acquire the lock I'm using java.util.concurrent.locks.ReentrantLock.ReentrantLock() I'm trying to acquire the lock…
0
votes
1 answer

Can a reentrant lock work in conjunction with a synchronized block?

I have a Data Transfer Object (DTO) in a Java application that is being read from and written to in several different threads across the application. Up until now I have been able to use synchronized(dto.class) for synchronization. There is now one…
0
votes
1 answer

How to release Java ReentrantLock after sometime no matter what

My objective is to avoid thread deadlock or starvation. I have the following sample code for using ReentranLocks: class X { private final ReentrantLock lock = new ReentrantLock(); // ... public void m1() { lock.lock(); // block…
tarekahf
  • 738
  • 1
  • 16
  • 42
0
votes
0 answers

Pattern that offers mutual exclusion, re-entrancy, distributed locking towards one (of many) APIs with HttpClient

I'm looking for an approach to locking that, by default, makes sure that all calls to a single API are run mutually exclusive using distributed locking. However, at the same time I need the option to instead lock larger blocks of code (critical…
0
votes
0 answers

ReentrantLock unlock wake up LockSupport park

I have a program which have multiple threads share the same lock, just like below: import java.util.concurrent.locks.LockSupport; import java.util.concurrent.locks.ReentrantLock; public class LockTest { private static final ReentrantLock lock =…
sdcyst
  • 1
  • 1
0
votes
1 answer

What is the use of Condition along with Lock in Java

I am trying to understand what is the use of doing condition.await() if I am already doing lock.lock() If I understood locks correctly, once I do lock.lock() it will not proceed any further if some other thread has a lock. So, in this case if…
figaro
  • 2,263
  • 3
  • 20
  • 27