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
3
votes
2 answers

BlockingQueue Implemetation using ReentrantLock

I was writing my own implementation of BlockingQueue for practice. I am trying to avoid using the synchronized keyword for the methods. I would instead like to use ReentrantLock. What is the best way to write this implementation? I am not a Java…
Hir
  • 83
  • 1
  • 7
3
votes
3 answers

Implementing blocking concurrency using ReentrantLock

I am attempting to implement a class to enforce concurrency in my java application by blocking asynchronous attempts to a modify a given instance of an entity (identified by a key) using RentrantLock. The goal is to block/queue multiple concurrent…
Strykker
  • 33
  • 1
  • 3
2
votes
3 answers

How to set/get string in Java safely?

I have read Java String class is immutable and thread-safe but I am still confused about whether the reference assignment for Strings is thread-safe or not. First question: If thread A calls Foo.setString() while thread B calls Foo.getString(), is…
Lee
  • 103
  • 1
  • 11
2
votes
2 answers

Reentrant lock implementation detail

I am trying to understand a particular detail in ReentrantLock::lock method. I am looking at it and seeing it as: final void lock() { if (!initialTryLock()) { acquire(1); } } So first it tries this method : initialTryLock (I will look…
Eugene
  • 117,005
  • 15
  • 201
  • 306
2
votes
1 answer

Java:Multiple thread safety issues: Using Thread extends and Lock

When I use Thread class and Lock to solve threads safety problems, there occurs 0 ticket! Code: package ThreadTest; import java.util.concurrent.locks.ReentrantLock; class ThreadOne extends Thread{ private static int ticket = 100; private…
2
votes
0 answers

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 each Object of the array is made up…
2
votes
1 answer

ReentrantLock threads terminating randomly

I have been working on a school assignment which is about multithreading in Java. One of the tasks that I am stuck on is that we need to create multiple threads in different groups, and once there are 4 threads in each group, only then they can be…
Naeem Khan
  • 950
  • 4
  • 13
  • 34
2
votes
2 answers

ReentrantReadWriteLock performing very bad compared to ReentrantLock

I have created 1000 threads to increment,1000 threads to decrement,1000 threads to read the value. Each increment thread ,increases value by 25000 times. Each decrement thread ,decreases value by 25000 times. Each read thread ,reads the value by…
2
votes
2 answers

Can FairSync guarantee the order of execution?

My first question, Thank for your help! I'm trying to print odd and even numbers 1~100 alternatively using two threads. Expected results: pool-1-thread-1=> 1 pool-1-thread-2=> 2 pool-1-thread-1=> 3 pool-1-thread-2=> 4 ...... …
student
  • 127
  • 5
2
votes
1 answer

Is this the correct approach to achieve concurrency?

I have created a small application which basically includes rest Apis for money transfer between accounts. For the simplicity I have used concurrent hashmap for datastore. Now the very basic concept to be implemented for this would be multi…
2
votes
1 answer

How to find out contention problems in java when most of the classes are concurrent

We used yourkit profiler to find out and resolve many contention issues in our application. We used thread monitoring to see which threads are blocked and resolved many of those issues. But yourkit does not show up ReentrantLocks as blocked or…
anony
  • 314
  • 2
  • 14
2
votes
1 answer

How will ReentrantLock object created inside a method's local scope work?

The above is a screen print from OCP 7 java se book. page 791. My question is if a new ReentrantLock object is created in a method every time and locked, how would that stop two threads from running the code block in between lock and unlock? Won't…
2
votes
1 answer

ReentrantLock lock and unlock by always the same thread

I am trying to implement Reentrant locks on multi-threads but for some reason, the same thread unlocks and then locks again resulting in always the same thread to run therefore the same operation. Below is the code how the threads are…
Eilleen
  • 357
  • 1
  • 16
2
votes
1 answer

Can some subclass from Thread call Condition's function signal() if any other Thread isn't call await()?

Example, two threads (Informer and Watcher) use same List. Informer add data in List and Watcher can't read collection if collection is empty or if Informer access the List. What will happen if Informer call function Condition.signal() but Watcher…
2
votes
2 answers

wait/notify vs sleep/interrupt vs ReentrantLock.Condition

I'm writing a search-as-you-type mechanism (android) that makes an sqlite query in a background thread and posts the results back to the UI thread. Ideally, the thread should wait/sleep, wake up to execute any received Runnable object and go back to…
Nick
  • 585
  • 4
  • 11