Questions tagged [reentrantlock]

ReentrantLock is a Java mutex class.

reentrantlock
50 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
7 votes
0 answers
1k views

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....
nightuser's user avatar
  • 686
5 votes
3 answers
1k views

ReentrantLock gets mysteriously unlocked within the same thread, even while no other threads are accessing it

I have some code that I thought would be very straightforward: public int internalWrite(byte[] data, int offset, int size) throws InterruptedException { lock.lockInterruptibly(); try { ...
meew0's user avatar
  • 51
2 votes
0 answers
329 views

Locking specific Indexes of an Array using Concurrent Locks - RentrantLock

So i'm planning on having multiple threads accessing a shared resource which is a 2D array. Each Object will access one index at a time to set the contents of the object as shown below. Cell is what ...
Helium's user avatar
  • 31
2 votes
0 answers
773 views

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 ...
Zim-Zam O'Pootertoot's user avatar
2 votes
1 answer
3k views

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 ...
szaman's user avatar
  • 21
1 vote
0 answers
142 views

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 ...
Kamil W's user avatar
  • 2,298
1 vote
0 answers
128 views

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 ...
ghostrider's user avatar
  • 2,146
1 vote
1 answer
92 views

Bow/Bower example in java oracle tutorial with lock object

In the java docs example, the lock.unlock() method is used twice, once in impendingBow() method and bow() method. My question is, shouldn't lock.unlock() method in impendingBow() method do the trick, ...
tanwir's user avatar
  • 11
1 vote
0 answers
402 views

Is this the right way to use ReentrantLock.isHeldByCurrentThread?

I need to exclude web access from a ReentrantLock lock, see webAccess() method below. I can not add any parameter to webAccess() api because in real world it is our framework api that I am not ...
Kai's user avatar
  • 3,835
1 vote
1 answer
231 views

Dead lock occur when assign multiple conditions to lock

My program require to print backward from 10 to 1. Each number print from one thread. It uses one lock with multiple condition objects. But the program cause a dead lock when I run it. This is my ...
vtc's user avatar
  • 66
1 vote
0 answers
250 views

ReentrantLock demo: signal & return value; => Unlocked?

I am trying to learn basic concepts of Java 8 Concurrent Programming and can not understand the Unlock with return in a demo snippet here : http://www.javatips.net/api/thinking-java-master/src/main/...
fmatz's user avatar
  • 21
1 vote
0 answers
727 views

difference between lock/unlock vs wait/notify in the OS

Title says it all, using a ReentrantLock, whats the difference between the lock unlock and using the condition with wait notify? I know wait/notify is used with a condition, and inside a loop, is ...
José Moreira's user avatar
1 vote
1 answer
437 views

How to wait for specified time in critical section locked by ReentrantLock?

I need to stop thread somehow for 1 sec while thread is in critical section locked by ReentrantLock. My code is : public class Lock implements Runnable { private ReentrantLock lock = new ...
Volodymyr Levytskyi's user avatar
1 vote
0 answers
103 views

Using a ReentrantReadWriteLock within another ReentrantReadWriteLock

I am not sure if I have implemented ReentrantReadWriteLock correctly so I would appreciate if you can let me know if I had done anything wrong. Scenario: Imagine that there are two steps required to ...
Tay Wen Bin's user avatar
1 vote
0 answers
129 views

Interleaved ReentrantLocks (for 3D camera movement, LWJGL)

My question is if is rather a valid choice to interleave multiple ReentrantLocks or not for a specific case: camera movement in 3D space (LWJGL, openGL) - with graphical representation of course. My ...
Florian R. Klein's user avatar
1 vote
1 answer
300 views

ReentrantLock shared by threads not seeming to honor lock

I have a single ReentrantLock object that I share among threads, but I'm able to lock the same object by different threads. This doesn't seem right as they are different threads. Here is the ...
user3133269's user avatar
0 votes
0 answers
43 views

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 ...
ole_sh_ka's user avatar
0 votes
1 answer
105 views

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(...
jlaufer's user avatar
  • 149
0 votes
0 answers
268 views

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 ...
Expotr's user avatar
  • 49
0 votes
0 answers
147 views

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 ...
sdcyst's user avatar
  • 1
0 votes
1 answer
314 views

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 ...
figaro's user avatar
  • 2,285
0 votes
0 answers
53 views

Java Syncronization on two objects results in dead lock

I need to implement some basic money transfer logic between accounts. For the transfer being consistent, I take advantage of syncronization lock. This is Account object: class Account { UUID id; ...
Hilmi Yamcı's user avatar
0 votes
0 answers
136 views

Why does the same thread keeps locking (reentrant lock) unless I use a sleep?

So, in the code below I'm trying to code a simple client with a demultiplexer. I have a thread responsible for receiving and storing in queues all TCP packets. This thread is obviously in a loop, ...
RYZ34M's user avatar
  • 1
0 votes
1 answer
42 views

LinkedBlockingQueue fullyLock() throw exception, fullyUnlock() won't execute

fullyLock() contains 2 locks:putLock and takeLock, they are all ReentrantLock and NonfairSync void fullyLock() { putLock.lock(); takeLock.lock(); } Will there be such a ...
jianjian's user avatar
0 votes
1 answer
25 views

ReentrantLock is giving unexpected results

I am running two variations of similar code, expecting same result to appear however the output shows unexpected results. variation1 with Synchronized block public class SomeTask implements Runnable { ...
Tirthankar Mitra's user avatar
0 votes
1 answer
36 views

Simulating traffic signal (a single box)

I wanted to implement a traffic signal (a single box). I saw there were many questions in SO, but I felt they were not complete code. The below is the full source. I am using lock and condition for ...
CuriousMind's user avatar
  • 8,591
0 votes
0 answers
43 views

Java socket chat server having message problem

i'm currently developing a chat server with java and sockets in the terminal, and it works fine, we can send messages and receive messages. The problem is if we are in the middle of writing a ...
Manuel0Borba's user avatar
0 votes
1 answer
38 views

How can I solve the problem with the consumer?

I'm trying to implement a program with a warehouse, consumer, and manufacturer using ReentrantLock. I have a cyclic queue based on an array as a storage of goods. The Producer can produce the product ...
Liza's user avatar
  • 1
0 votes
2 answers
140 views

SharedArray Deadlock and raise Conditions

I'm trying to stimulate the scenario of deadlocks in the shared array using Reentrant Locks. class SharedArray { private int ff[]; private Lock keys[]; public SharedArray(int n){...
Hassaan's user avatar
  • 3,941
0 votes
2 answers
291 views

Deadlock on ReentrantLock in interceptor

I have next problem. When my token is expired I try to refresh it. If this refresh code called by single-thread it works fine, but if there are two or more threads sometimes I catch deadlock. This is ...
Artem's user avatar
  • 4,599
0 votes
1 answer
562 views

Java 8 - ReentrantLock/Locking an object

I'm wondering if using a ReentrantLock is the solution for my problem; I'm trying to "lock"(prevent other threads from accessing/using it) an object until a certain operation is complete, and then ...
leapinglasagne's user avatar
0 votes
1 answer
295 views

How to make long waiting thread to run

I am working in core java. I have one small program which will print numbers 1,2,3 by corresponding threads thread1 , thread2 and thread3. while(true) { synchronized (obj) ...
Vaithilingam K's user avatar
0 votes
1 answer
492 views

Reentrant Lock , busy waiting resolved by the new tryLock method

I was going through java doc description of lockInterruptibly method in ReentrantLock class. My intention was to see if the threads waiting to acquire lock are getting interrupted, may be I am doing ...
Explorer's user avatar
0 votes
0 answers
47 views

How can one make a routine, which uses global variables and blocks on a semaphore, a re-entrant routine?

The routine uses global variables The routine blocks on a semaphore, sleep The routine should be re entrant.
user2655050's user avatar
0 votes
1 answer
307 views

ReentrantLock tryLock()

This is the source code of ReentrantLock#tryLock: public boolean tryLock() { return sync.nonfairTryAcquire(1); } My question is that: There are two types of Synchronizers: FairSync and ...
jimwi's user avatar
  • 33
0 votes
2 answers
979 views

Recursive Synchronization vs Recursive Reentrant Lock

I know thread is allowed to acquire a monitor owned by itself i.e. In Java, synchronized locks are reentrant as shown in example below. My query is if i use java.util.concurrent.locks.ReentrantLock ...
Joker's user avatar
  • 11k
0 votes
1 answer
263 views

Android Service and worker thread ReentrantLock infinite loop

Hi I want to get rid of an infinite loop which holds a worker thread in a service. I try to use the ReentrantLock() methods but I can't seem to get it to work. My worker thread calls from JNI the ...
Zimgir's user avatar
  • 101
0 votes
1 answer
538 views

Issue with Producer-Consumer using Lock and Condition

I have implemented Producer-Consumer program using ReentrantLock and Condition. My implementation runs without any error if I start the Producer thread first. But if I start the Consumer thread first ...
user6604211's user avatar
0 votes
0 answers
418 views

Java Locks/Condition Variable - signal() not waking up the thread

so been stuck here, tried to look for answers, couldn't find any. Simply, I keep trying to get the Train to signal to the Passenger that its in the station. Signal keeps looping but passenger just ...
Dionne's user avatar
  • 11
0 votes
0 answers
255 views

Why ConcurrentHashMap use 'synchronized' inside computeIfAbsent

Inside 'computeIfAbsent' method of ConcurrentHashMap there is code: synchronized (r) { if (casTabAt(tab, i, null, r)) { ... Why not 'ReentrantLock' ?
littleAlien's user avatar
0 votes
1 answer
204 views

On what object Reentrant lock aquire lock on?

Hello I have question about ReentrantLocks in Concurrent package. //First Object ob = new Object(); synchronized(ob){ } //Second Lock lock = new ReentrantLock(); lock.lock(); try{ } finally{ lock....
Lakshitha Ranasinghe's user avatar
0 votes
0 answers
507 views

Java ReentrantLock issue

Hello I'm having some issues regarding Lock's implementation: ReentrantLock. I'm using a singleton cache inside Spring 3.0(web). However when I open several browser tabs at the exact same time, all ...
delpo's user avatar
  • 230
0 votes
1 answer
179 views

What's the benefit of having two ReentrantLock in a class?

If I have this code: public class PrLock { private Lock lock1= new ReentrantLock(); private Lock lock2= new ReentrantLock(); private int num=0; public void addLock1(){ lock1....
gluckko's user avatar
0 votes
0 answers
50 views

Avoiding multiple lock attempts java

I have the following method: public foo bar(String id) { Lock localLock = lockUtil.lock(id); try { do something; return foo; } finally { lock.unlock(); } } I ...
user3791554's user avatar
0 votes
2 answers
1k views

Query about disadvantage of Reentrant locks over synchronized block

I am reading comparison between Reentrant locks and synchronization blocks in java. I am going through the various resources on internet. One disadvantage that I discovered using Reentrant locks over ...
Vinit89's user avatar
  • 583
0 votes
0 answers
750 views

FIFO for Write Order in ConcurrentHashMap

My CHM already contains following Data -> 1 Apple 2 Banana 3 Cat 4 dog 1,2,3,4 are keys and Apple, banana... are keys correspondingly. If 3 threads t1, t2, t3 wants to modify the same ...
HakunaMatata's user avatar
0 votes
0 answers
235 views

Lock using ReentrantLock in java

I'm trying to implement at playlist in java. The only problem is that populatePlaylist() is a bit slow and needs to be locked. I have written this code but it doesn't seem to work. private ...
Markus's user avatar
  • 2,554
0 votes
0 answers
1k views

Wrapping ExecutorService to provide custom execution

I want to write a reusable piece of code to allow waiting conditions while submitting tasks to an executor service. There are alot of implementaions for neat ways of blocking if too many tasks are ...
Franz Kafka's user avatar
  • 10.8k
-3 votes
1 answer
55 views

How do re-entrant locks work in this piece of Java code?

I have three classes one is main class, one is a shared resource class and other is thread class. I am facing issue in understanding the code. How can I understand this code, and how can I analyse the ...
Shivani Gupta's user avatar
-4 votes
1 answer
290 views

Java Reentrant Locks: Second thread can't get lock

I have just started using synchronous methods in Java and I have made a basic application that uses an reentrant lock this is what it is suppose to do: One thread will access the locked class first ...
I                         I's user avatar
  • 15