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

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 deciding which thread will actually…
CuriousMind
  • 8,301
  • 22
  • 65
  • 134
0
votes
0 answers

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 sentence and another message is received it interrupts…
0
votes
1 answer

Why the code is not resulting in a deadlock ALWAYS, trying to understand the working of Reentrant locks vs synchronized blocks?

I have two pieces of code . First one uses synchronized blocks and it causes a deadlock which is completely understandable. In the second code i tried to recreate the same issue , but using Reentrant locks this time. But the second code does not…
0
votes
1 answer

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 up to n times. I faced the problem that the…
0
votes
1 answer

Interrupt all waiting threads with Reentrant lock Java

Is there any way in which I could interrupt all the threads that are waiting for lock if the thread which already acquired lock has some exception?
0
votes
1 answer

Why else condition not shows over console for waiting Thread. if i'm using future.get() method?

Here in office instance getsum() returns sum of n number digits and only one Thread allowed to complete the calculation at a time with a sleep of 1 seconds. During this time other Thread will try for lock, If Thread not get lock it prints else…
0
votes
1 answer

based on which implementation does await and signalAll work

I was reading about the reentrantlock in java and how we can know the lock condition by using the newCondition() method in the interface Condition but then I saw in the documentation of the interface Condition that the user has to provide an…
StudentAccount4
  • 186
  • 1
  • 11
0
votes
1 answer

Why AbstractOwnableSynchronizer.exclusiveOwnerThread is not declared as volatile?

While reading the source code of java.util.concurrent.locks.ReentrantLock I found that the tryLock() method is implemented as below: final boolean nonfairTryAcquire(int acquires) { final Thread current = Thread.currentThread(); …
yangty89
  • 87
  • 5
0
votes
1 answer

IllegalMonitorStateException while calling notifyAll

I have a piece of code which is giving IllegalMonitorException while calling notifyAll. While enQueue(), thread add the data and throws illegalMonitorException. BlockingQueue.class : public class BlockingQueue { private Queue queue = new…
0
votes
1 answer

ReentrantLock with CompletableFuture

Before the world of CompletableFuture, if I wanted to lock a variable, I could do this: private ReentrantLock myLock = new ReentrantLock(); private List myLockableList = new ArrayList<>(); public void doStuff() { try { …
0
votes
1 answer

Read Firebase in Synchronously

I am trying to create a method to read my Firebase database in line with the rest of my code. I am trying to use a Reentrant Lock to do this, but it is not working. I want the output to be ---------- A ---------- B ---------- C ---------- D upon…
0
votes
1 answer

Can i modify ReentrantLock so one can use it with try with resources?

I wanted to ask if this implementation is ok, or maybe there are some issues that can be later problematic. The idea is to implement a ReentrantLock class that can be used with 'try with resources' and that means that it has to implement the…
0
votes
1 answer

Is lock.tryLock() thread Safe?

I tried the thread race program with Synchronization,lock.lock() and lock.tryLock(),I found that with synchronization and lock.lock() works fine,but lock.tryLock() itself is not thread safe.This method is not able to acquire the lock and hence…
0
votes
2 answers

Future.cancel and ReentrantLocks

Scenario I have this class, let's say Foo, whose only job is to sequentially execute a set of tasks. Sounds simple, right? Well, all of these tasks are executed in their own separate thread. Instead of simply calling Thread.join() on each task to…
mre
  • 43,520
  • 33
  • 120
  • 170
0
votes
1 answer

Why does a Java thread have to reenter into lock if it is already taken by that thread?

I was reading about Java threads and Reentrant Locks. ReentrantLock allow threads to enter into lock on a resource more than once. My question is why does the thread have to do so, if it already has a lock on the resource ? What is the purpose of…
codechaser
  • 91
  • 1
  • 2