I'm new to multithreading and having a problem with threads and locking in my program.
I have simplified my problem by creating the code below
private final ConcurrentLinkedQueue<String> valueQueue = new ConcurrentLinkedQueue<String>();
private final Object lock = new Lock();
private ProcessValue processor = new ProcessValues();
public void addValue(String value){
synchronized (lock) {
valueQueue.add(value);
lock.notify();
}
}
public void waitForValuesToBeAdded(){
synchronized (lock) {
lock.wait();
executeValues();
}
}
public void executeValues(){
synchronized (lock) {
processor.processValues();
valueQueue.clear();
lock.notify()
}
}
When the program starts it executes waitForValuesToBeAdded. As the name suggests this just waits for values to be added to the queue. When values are added we no longer wait and so executeValues() is called.
When processor.processValues() is executing I don't want values to be added to the Queue (valueQueue.add(value)). I need them to wait until processValues() has finished.
I thought if I use a synchronized block it will block the insertion but when I add another value while processValue is executing everything hangs.
Have I missed something in my program?
Try using jvisualvm to see where threads are blocked. You can do a "thread dump" to analize this.
Rewrite as follows:
private final ConcurrentLinkedQueue<String> valueQueue = new ConcurrentLinkedQueue<String>();
private final Object lock = new Lock();
private ProcessValue processor = new ProcessValues();
public void addValue(String value) {
synchronized (lock) {
valueQueue.add(value);
lock.notifyAll();
}
}
public void waitForValuesToBeAdded() throws InterruptedException {
synchronized (lock) {
while (valueQueue.size() == 0) {
lock.wait();
}
executeValues();
}
}
public void executeValues() {
synchronized (lock) {
processor.processValues();
valueQueue.clear();
lock.notifyAll()
}
}
Related
I know that there are a lot of similar questions, but no one helped me. I am getting IllegalMonitorStateException: object not locked by thread before wait() when I try to pause the thread.
Here is my initialization method:
// called only once in constructor; the variables are global ( private Thread ... )
public void init() {
recordingThread = new Thread(new Runnable() {
#Override
public void run() {
isNewRecordingThread= false;
record();
}
});
recognitionThread = new Thread(new Runnable() {
#Override
public void run() {
isNewRecognition= false;
recognize();
}
});
...
}
startRecording method:
private synchronized void startRecording(Thread recordingThread) {
if(isNewRecordingThread){
recordingThread.start();
return;
}
recordingThread.notify();
}
startRecognition method:
private synchronized void startRecognition(Thread recognitionThread) {
shouldContinueRecognition = true;
if(isNewRecognition){
recognitionThread.start();
return;
}
recognitionThread.notify();
}
And the stopping method where I actually get the error:
private synchronized void stopRecordingAndRecognition(Thread recordingThread, Thread recognitionThread) {
try{
if (recordingThread != null && recordingThread.isAlive()) {
recordingThread.wait();
}
if (recognitionThread != null && recognitionThread.isAlive()) {
recognitionThread.wait();
}
} catch (InterruptedException e){
Log.d("TESTING","InterruptedException e= "+e);
}
}
"object not locked by thread before wait()"
Think, what object is meant in this message? That very object for which wait() is applied:
recordingThread.wait();
that is, recordingThread.
synchronized void stopRecordingAndRecognition is irrelevant because it locks this object, and not recordingThread.
So, there are 2 solutions:
force methods to synchronize on recordingThread
embed synchronized methods into the class of recordingThread
"The goal is to start thread, pause it and after resume"
Pausing and resuming threads in Java is a bad practice because leads to subtle and hard to debug errors.
The only reliable way to stop/resume some computational process is to split this process into parts, process that parts in a loop, and before the start of processing the next part, checking if processing is allowed.
As an evolution of this approach, each part is formed as a Runnable and is submitted to a single-threaded Executor. Instead of stop and resume the Executor, producer thread(s) simply stop and resume submitting the partial tasks to the executor.
If (some) parts can be processed in parallel, then multithreaded executor can be used, but it requires coordination between submitting particular tasks.
The goal is to start thread, pause it and after resume
Here is a code snippet that I use for suspending and resuming a thread.
public class ThreadStatus {
private boolean paused;
private final String threadName;
private final ReentrantLock lock;
private final Condition condition;
public ThreadStatus (String name) {
threadName = name;
lock = new ReentrantLock();
condition = lock.newCondition();
paused = false;
}
// check for the thread to be paused
public void checkForPause() {
lock.lock();
try {
while (paused) {
condition.await();
}
} catch (InterruptedException ie) {
// interrupted
} finally {
lock.unlock();
}
}
// Pause the thread
public void pause() {
lock.lock();
try {
paused = true;
} finally {
lock.unlock();
}
}
// Resume the thread
public void resume() {
lock.lock();
try {
paused = false;
condition.signalAll();
} finally {
lock.unlock();
}
}
#Override
public String toString() {
return threadName;
}
}
If you need you could implement isStopped() or isRunning() similarly.
final ThreadStatus threadStatus = new ThreadStatus("Thread-A");
In the client code, call threadStatus.checkForPause() at the relevant point. For example, if you have some repeated processings inside a loop, you can do something like -
while (!threadStatus.isStopped()) {
threadStatus.checkForPause();
// do your processing here
}
I have an object:
public class Resource {
private Lock lock = new ReentrantLock();
private boolean processed = false;
public Lock getLock() {
return lock;
}
public boolean isProcessed() {
return processed;
}
public void setProcessed(boolean processed) {
this.processed = processed;
}
}
I want to stop the thread "one" untill the thread "two" changes the variable "processed" to true. After "processed" is set to true I want to wake up the thread "one" and continue doing some stuff.
I know that we can use wait and notify methods to organize it but it is very dangerous because of interruptions.
If I will use only wait and notify methods there may be a situation when I wait infinity.
If our wait method is interrupted by some reason, we check that the "process" variable is still false after that we can use wait again like here:
while(true){
if(!resource.isProcessed()){
resource.getLock().wait();
}
else{
break;
}
}
It is dangerous to use the code like this because after we checked "!resource.isProcessed()" and before we use "resource.getLock().wait()" another process can set the "process" to true and call "resource.getLock().notify()" (which will not take any effect because we haven't yet called "wait()").
How to wait for some condition safely? How to notify/unlock safely some condition?
As Peter Lawrey answered in comments there are Condition available in java. (Thank you for pointing)
Here is a copy past of the example which is available in the documentation:
class BoundedBuffer {
final Lock lock = new ReentrantLock();
final Condition notFull = lock.newCondition();
final Condition notEmpty = lock.newCondition();
final Object[] items = new Object[100];
int putptr, takeptr, count;
public void put(Object x) throws InterruptedException {
lock.lock();
try {
while (count == items.length)
notFull.await();
items[putptr] = x;
if (++putptr == items.length) putptr = 0;
++count;
notEmpty.signal();
} finally {
lock.unlock();
}
}
public Object take() throws InterruptedException {
lock.lock();
try {
while (count == 0)
notEmpty.await();
Object x = items[takeptr];
if (++takeptr == items.length) takeptr = 0;
--count;
notFull.signal();
return x;
} finally {
lock.unlock();
}
}
}
You can use a CountDownLatch to make one thread await until an operation performed by another thread is completed.
Let's suppose T1 and T2 are your threads and they share a CountDownLatch initialized with a counter of 1. T1 will first await() on the latch, while T2 should perform its operation and then invoke countDown() on the latch to let T1 proceed.
Of course await() in T1 can still be interrupted, so you may want to call it in a loop.
class T1 implements Runnable {
private final CountDownLatch latch;
T1(CountDownLatch latch) {
this.latch = latch;
}
public void run() {
awaitUninterruptibly(latch);
doWork();
}
private void awaitUninterruptibly(CountDownLatch latch) {
boolean interrupted = false;
try {
while (true) {
try {
latch.await();
return;
} catch (InterruptedException e) {
interrupted = true;
}
}
} finally {
if (interrupted) {
Thread.currentThread().interrupt();
}
}
}
}
class T2 implements Runnable {
private final CountDownLatch latch;
T1(CountDownLatch latch) {
this.latch = latch;
}
public void run() {
doWork();
latch.countDown();
}
}
I am writing producer and consumer code using wait() and notify() in Java.
Thread-0 is created and is invoked on produce() and Thread-1 is created and is invoked on consume().
public class Processor {
private volatile List<Integer> list = new ArrayList<>();
private final int MAX_CAPACITY = 5;
Object lock = new Object();
public void produce() throws InterruptedException {
while (true) {
while (list.size() == MAX_CAPACITY) {
System.out.println("List is full! Producer is Waiting....");
synchronized (lock) {
lock.wait();
}
}
synchronized (lock) {
int random = new Random().nextInt(100);
list.add(random);
System.out.println("Added to list:" + random);
lock.notify();
}
}
}
public void consume() throws InterruptedException {
while (true) {
while (list.size() == 0) {
System.out.println("List is empty!! Consumer is Waiting...");
synchronized (lock) {
lock.wait();
}
}
synchronized (lock) {
int i = list.remove(0);
System.out.println("Removed from list:" + i);
lock.notify();
}
}
}
}
The problem is that during execution, program stops after produce():
List is empty!! Consumer is Waiting...
Added to list:22
Added to list:45
Added to list:72
Added to list:91
Added to list:51
List is full! Producer is Waiting....
I am not able to understand what's the problem here. I somehow figured out that wrapping the code from while loop in synchronized block in produce() and consume() solves the problem.
produce()
synchronized (lock) {
while (list.size() == MAX_CAPACITY) {
System.out.println("List is full! Producer is Waiting....");
lock.wait();
}
consume
synchronized (lock) {
while (list.size() == 0) {
System.out.println("List is empty!! Consumer is Waiting...");
lock.wait();
}
}
What is the issue here? Is it a case of Thread starvation or deadlock?
Edit: Calling class:
public class App {
public static void main(String[] args) {
final Processor processor = new Processor();
Runnable r1 = new Runnable() {
#Override
public void run() {
try {
processor.produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Runnable r2 = new Runnable() {
#Override
public void run() {
try {
processor.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();
}
}
When you perform list.size() it is not thread safe and there is no guarentee you will ever see the value changed in another thread. The JIT could even inline the value if it detects you are not changing it in that thread.
By placing the synchronized block outside the loop you ensure a change in the value is visible (as it is also inside the while(true) loop.
Using synchronized outside loop creates read barrier. Therefore producer/consumer will see latest list inside loop where you are checking list.size(). That is why it works after you move while loop inside synchronized block.
In your case I would also suggest you to use single synchronized block in producer/consumer.
For example in your implementation if list.size() == 0 becomes false for consumer, it will release lock on lock object then in the next statement try to re-acquire lock again for consuming the data, which is unnecessary and inefficient. It should be like:
public void consume() throws InterruptedException {
while (true) {
synchronized (lock) {
while (list.size() == 0) {
System.out.println("List is empty!! Consumer is Waiting...");
lock.wait();
}
int i = list.remove(0);
System.out.println("Removed from list:" + i);
lock.notify();
}
}
}
Just for practice I wanted to implement the java synchronized keyword as a java object.
Would you say the code below is a good design for this?
I guess AtomicReference would have a similar performance to AtomicBoolean?
Updated code after suggestions:
public class SynchronizedBlock implements Runnable{
private final Lock lock;
private final Runnable runnable;
public SynchronizedBlock(Runnable r, Lock l){
runnable = r;
lock = l;
}
public void run() {
try {
while(!lock.compareAndSet(false, true)){
Thread.sleep(100);
}
runnable.run();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
public class Lock {
private final AtomicReference<Boolean> locked = new AtomicReference<Boolean>(false);
public boolean compareAndSet(boolean expected, boolean update){
return locked.compareAndSet(expected, update);
}
public boolean isLocked(){
return locked.get();
}
public void unlock(){
locked.set(false);
}
}
#Test
public void test() {
final SynchronizedBlock sb = new SynchronizedBlock(new Runnable(){
public void run() {
x++;
System.out.println(x);
}
}, new Lock());
Runnable r1 = new Runnable(){
int c = 0;
public void run() {
while(c<10){
sb.run();
c++;
}
}
};
Runnable r2 = new Runnable(){
int c = 0;
public void run() {
while(c<10){
sb.run();
c++;
}
}
};
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();
while (t1.isAlive() && t2.isAlive()){
}
assertEquals(20,x);
}
You should add a method to encapsulate the compareAndSwap, and there is no point looping for the lock to be free before attempting to obtain it. Why get in the situation where you can see the lock is free but by the time you try to take it, it is gone.
I would remove the lock method and place the unlock in a finally lock so that an Exception/Error doesn't result in a lock which never unlocks.
Also I would use an AtomicBoolean which is more natural than an AtomicReference
Firstly and most importantly, you should remove Thread.sleep(100). This will cause at least 100ms latency even in only 2-thread contention.
You can simply use AtomicBoolean instead of AtomicReference to simplify your code. Also if you're really concerned about concurrency in high-contention situation, you can modify your code to check if it's locked before doing CAS.
while (true) {
if (lock.isLocked()) continue; // or get() == true if you use AtomicBoolean
if (lock.compareAndSet(false, true))
break;
}
This is an example of TTAS(Test-Test-And-Set) locking which takes advantage of local-spinning to reduce main-memory access while looping.
See http://en.wikipedia.org/wiki/Test_and_Test-and-set
I have created a new Condition chopstickFree and in my pickUpChopstick() method, I am waiting for a lock on it but I can't get access to it at all.
Through debugging I have found that when it gets to the chopstickFree.await() line in the pickUpChopstick() method, it just pauses indefinitely
I don't understand? That code in the constructor was just an unsure attempt to get it working but either way, I have created a new condition, signaled to all that it is free, but I can't get a lock on it at all?
public class Chopstick {
Lock lock = new ReentrantLock();
private Condition chopstickFree = lock.newCondition();
private Condition chopstickInUse = lock.newCondition();
Chopstick() {
lock.lock();
chopstickFree.signalAll();
lock.unlock();
}
// Pick up chopstick
public void pickUpChopstick() throws InterruptedException {
lock.lock();
try {
chopstickFree.await(); // ALWAYS PAUSES HERE INDEFINITELY
chopstickInUse.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
// Release chopstick
public void releaseChopstick() {
lock.lock();
chopstickFree.signal();
lock.unlock();
}
}
Any ideas?
Cheers
Condition#signalAll() only signals threads that are currently in Condition#await() (or its friends), i.e. the signal isn't queued up for later calls.
You need another flag protected by the lock to correctly implement:
public class Chopstick {
private final Lock lock = new ReentrantLock();
private final Condition chopstickFree = lock.newCondition();
private volatile boolean isFree = true;
Chopstick() { /* Nothing */ }
// Pick up chopstick
public void pickUpChopstick() throws InterruptedException {
lock.lock();
try {
while (!isFree) {
chopstickFree.await();
}
isFree = false;
} finally {
lock.unlock();
}
}
// Release chopstick
public void releaseChopstick() {
lock.lock();
try {
isFree = true;
chopstickFree.signal();
} finally {
lock.unlock();
}
}
}
Here is a version using a Semaphore that might be a little closer in intent to your original implementation:
public class Chopstick {
private final Semaphore chopsticksAvailable = new Semaphore(1);
Chopstick() {
// Nothing
}
// Pick up chopstick
public void pickUpChopstick() throws InterruptedException {
chopsticksAvailable.acquire();
}
// Release chopstick
public void releaseChopstick() {
chopsticksAvailable.release();
}
}