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
0
votes
0 answers

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; double balance; } This is inside transfer…
Hilmi Yamcı
  • 463
  • 8
  • 20
0
votes
0 answers

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, where it locks the map that contains the queues,…
RYZ34M
  • 1
  • 1
0
votes
1 answer

java ReentrantLock tryLock() misunderstanding

ReentrantLock l = new ReentrantLock(true); Reader[] readers = new Reader[2]; for (int i = 0; i < readers.length; i++) { readers[i] = new Reader(l); } for (int i = 0; i <…
javaway
  • 127
  • 2
  • 8
0
votes
1 answer

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 situation : In remove() method, putLock.lock() success,…
0
votes
1 answer

kotlin object lock

I want to use synchronized(object lock) at Kotlin, but idk how to use synchronized at Kotlin. I already search for the usage of synchronizing at Kotlin but ReentrantLock can't lock objects that I guess. please help me I am stuck with this 2 days…
vjh0107
  • 107
  • 10
0
votes
1 answer

Java ReentrantLock. Unlock method does not reset the header, how to remove the executed thread wrapped in a node?

I am learning Java ReentrantLock source code these days. On the unlock method, it actually do two things: (1) Check if acquire number == release number, if yes, set ExclusiveOwnerThread as null; (2) if (1) is OK, unpark successor; public final…
Machi
  • 403
  • 2
  • 14
0
votes
0 answers

Is it bad practice to use ReentrantLock instead of Synchronized?

I like to use ReentrantLock even when the same result could be achieved with synchronized. I don't really do it for performance but just because I prefer to use the try-catch-finally block of locks. The question is: is it acceptable to use…
Spyromancer
  • 435
  • 1
  • 3
  • 10
0
votes
1 answer

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 { public int count = 0; @Override …
0
votes
2 answers

ReentrantReadWriteLock readLock giving time limit exceeded

I'm trying to learn concurrency I was playing around with below code: https://leetcode.com/problems/lru-cache/discuss/724784/Detailed-Explanation-with-Threadsafe-LRU-Cache-in-Java class LRUCache { /** Algorithm : …
MrRobot9
  • 2,402
  • 4
  • 31
  • 68
0
votes
1 answer

Prioritization on ReentrantLock's Condition

Problem: I have a set of Threads some of which must take a priority to other in acquiring ReentrantLock. The solution: I can imagine to have a fair ReentrantLock with 2 Condition queues: lowPriority and highPriority. The point is highPriority is…
Some Name
  • 8,555
  • 5
  • 27
  • 77
0
votes
1 answer

Java Reentrant lock dead lock

Why java reentrant lock does not cause dead lock? I was studying Reentrant lock, and did realize that a thread failed to try acquire will be placed in a sync queue, and before it parks, it set its prev node's waitStatus to SIGNAL, and recheck if the…
goodGuy
  • 51
  • 1
  • 6
0
votes
1 answer

Java AbstractQueuedSynchronizer and the template method pattern

While reading the source code of ReentrantLock, I found that internally it use a synchronizer which extends AbstractQueuedSynchronizer to control the lock. Doug Lea mentioned in this paper that AbstractQueuedSynchronizer serves as a "template method…
0
votes
1 answer

Java : ReentrantLock doesn't work It still mixing with each other

I can't figure out why the code doesn't work properly. The threads still mixing with each other. I have class A : package com.company; public class A implements Runnable { ReentrantLockClass r; public A(ReentrantLockClass r) { …
Laila Mattar
  • 361
  • 4
  • 19
0
votes
1 answer

User level Locking Approach With synchronizedMap & ReentrantLock

I want to achieve user based locking as shown in below code. If on user "1234" (java.lang.String.class identifier) some process is happening, or user is being used by some process, other processes should wait for that process to complete. Simple as…
ringdings
  • 30
  • 5
0
votes
2 answers

Threads not getting stored in sequence in set

I'm trying to add 10 threads to a set and name them sequentially. But when I debug to see the set, threads are not stored in sequential order. Can someone tell what I am doing wrong? Set allThreads = new HashSet(); final…