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
4 answers

Understanding deadlock with a simple example

I am working on understanding deadlock basics so I came up with below code. I have two threads acquiring locks in opposite order but they're not deadlocking. When I run it I see all the printouts. What am I doing wrong? public class DeadlockBasics…
flash
  • 1,455
  • 11
  • 61
  • 132
1
vote
1 answer

Example demonstrating the use of ReadWriteLock

I am trying to understand the concept of ReadWriteLock and implemented the below simple example. From what I have understood, whenever a thread has a readlock, another thread can acquire the readlock. But for a thread with writelock, no other thread…
ghostrider
  • 2,046
  • 3
  • 23
  • 46
1
vote
2 answers

ReentrantLock - Concurrent money transfer operation

While I was reading some concurrency code samples on the internet I found this one (money transfer operation between 2 bank accounts): class Account { double balance; int id; public Account(int id, double balance){ …
1
vote
1 answer

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, why use it again in bow() method? The code below…
1
vote
3 answers

how does locking a reentrant lock again helps when we already have the lock on it?

So , a reentrant lock increments the count by one if the current thread acquires the lock again. what i am unable to understand is why and how that helps or benefits us?
Karan
  • 23
  • 4
1
vote
0 answers

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 supposed to change. To exclude webAccess from the lock,…
Kai
  • 3,775
  • 10
  • 39
  • 54
1
vote
4 answers

Producer Consumer using Reentrant lock not working

I am trying to implement consumer-producer using a ReentrantLock as given below: class Producer implements Runnable { private List data; private ReentrantLock lock; Producer(List data,ReentrantLock lock) { …
learner
  • 4,614
  • 7
  • 54
  • 98
1
vote
1 answer

Handle multiple tryLock() calls with a thread

As a method of synchronizaton between threads, in Java we can use the ReentrantLock class. It has a method called tryLock() which allows the thread not to wait if it notices that a thread has not been acquired yet. As it returns a boolean value, it…
Le Sir Dog
  • 95
  • 1
  • 10
1
vote
1 answer

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 program. This class working fine import…
vtc
  • 66
  • 5
1
vote
1 answer

Renentrant tryLock() not locking in multi threaded enviroment

This is regarding tryLock() method in re entrant locks. I am running the below sample code. I understand that this code will run into a deadlock. import java.util.concurrent.locks.ReentrantLock; public class TestMain { public static void…
ZerekSees
  • 91
  • 1
  • 10
1
vote
2 answers

How to take Re-entrant lock on String value?

I have a multi-threaded application and I want to use re-entrant lock on a String. For example if I use normal synchronization my code will look like. I don't want two threads of same IPaddress to enter in my loadBalance() so I take lock on IP…
T-Bag
  • 10,916
  • 3
  • 54
  • 118
1
vote
0 answers

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 :…
fmatz
  • 21
  • 1
  • 2
1
vote
1 answer

Java locks: Hand over hand locking through list

I'm trying to understand the java.util.concurrent.locks library and wanted to implement two threads which run through a list, whereas the second thread should not overtake (take the lead) the first thread. Specifically, I wanted to implement…
f.k
  • 85
  • 2
  • 9
1
vote
0 answers

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 only that the difference? How does the OS threats…
1
vote
1 answer

Java Lock Condition Wait and Notify: IllegalMonitorStateException

I am new to Java. I just read the "core java" book. I met a issue about 'Condition & Lock'. I typed a piece of code from the book to the eclipse to do some practice. When I run the code ,the line of "sufficientFund.wait();" throws an…
gfan
  • 1,027
  • 1
  • 14
  • 28