I'm getting interlocked threads in somewhat simple producer/consumer (and based on examples correct) code.
There is a thread executing this:
public void append(final Object obj) {
buffer.add(obj);
if (buffer.size() >= BUFFER_MAX_SIZE) {
insertLock.lock();
switchLock.lock();
insertLock.unlock();
bufferFull.signal();
try {
bufferSwitch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
switchLock.unlock();
}
}
There is another thread with this code:
try {
insertLock.lock();
while (true) {
switchLock.lock();
insertLock.unlock();
bufferFull.await();
switchBuffers();
bufferSwitch.signal();
insertLock.lock();
switchLock.unlock();
if (insertBuffer.size() > 0) {
db.insert(insertBuffer);
insertBuffer.clear();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
As I mentioned it is based on the producer/consumer example on the Condition API documentation. I can't detect why both threads get stuck on the conditions await method.
Is there any error ? Looks like there is something my naked eyes can't see.
Thank you,
PS: Added working code.
Thread A does
insertLock.lock();
switchLock.lock();
and Thread B does
switchLock.lock();
....
insertLock.lock();
So if Thread A acquires insertLock while B gets switchLock, neither A nor B can proceed to the next line.
It's a clasic deadlock situation. You should always make sure the locks are getting locked in the same order.
For a producer/consumer problem with threads you could look at the BlockingQueue interface and its derived classes that java offers.
For example:
package cl.mds.migracion;
import java.util.concurrent.ArrayBlockingQueue;
public class Example {
static ArrayBlockingQueue<String> buffer = new ArrayBlockingQueue<String>(5);
static class Producer implements Runnable{
#Override
public void run() {
for (int i = 0; i < 10; i++){
try {
Thread.sleep(500); //seleep 500ms to simulate producer time
buffer.put(String.valueOf(i)); //put waits the thread until there is size in the queue.
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
static class Consumer implements Runnable{
public void run() {
for (int i = 0; i < 10; i++){
try {
Thread.sleep(100); //seleep 100ms to simulate slower consumer tha producer
System.out.printf("Consuming %s ....%n",buffer.take()); //take waits the thread until there is something in the queue
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args){
System.out.printf("Starting producer/consumer ....%n");
new Thread(new Producer()).start();
new Thread(new Consumer()).start();
System.out.printf("Finishing ....%n");
}
}
Related
I am understanding wait() in Java in regards to multithreaded, and as per the documentation, wait() should always be in a loop.
I have difficult in understanding what is the condition we have to give in the loop. Typically, I have seen:
synchornized(obj) {
while(some_condition) {
obj.wait();
}
// some other code
}
I am having difficulty in understanding the "condition" which is used in the loop within which we are keeping wait().
I tried to implement a scenario in which I created two different threads (two different classes implementing Runnable interface), for printing Odd and Even numbers, like: 1 ,2 ,3,4,5,6...
As this is inter-thread communication and we need synchronization, I am having difficulty in relating what is the condition on which I have to keep wait() in loop for these two different threads.
Any clues as to how to decipher this (the condition we keep in loop) greatly appreciated.
Here, maybe this few lines will push you in the right direction, as a follow up to my previous comments.
class LastPrintedMonitor {
public boolean wasLastEven = false;
}
class PrinterOdd implements Runnable {
LastPrintedMonitor monitor;
public PrinterOdd(LastPrintedMonitor monitor) {
this.monitor = monitor;
}
#Override
public void run() {
for (int i = 2; i < 40; i += 2) {
synchronized (monitor) {
while (!monitor.wasLastEven) {
try {
monitor.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(i);
monitor.wasLastEven = false;
monitor.notifyAll();
}
}
}
}
class PrinterEven implements Runnable {
LastPrintedMonitor monitor;
public PrinterEven(LastPrintedMonitor monitor) {
this.monitor = monitor;
}
#Override
public void run() {
for (int i = 1; i < 40; i += 2) {
synchronized (monitor) {
while (monitor.wasLastEven) {
try {
monitor.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(i);
monitor.wasLastEven = true;
monitor.notifyAll();
}
}
}
}
public class EvenOddPrinterDemo {
public static void main(String[] args) {
LastPrintedMonitor monitor = new LastPrintedMonitor();
Thread odd = new Thread(new PrinterOdd(monitor));
Thread even = new Thread(new PrinterEven(monitor));
odd.start();
even.start();
try {
odd.join();
even.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Done!");
}
}
You mentioned two classes so their synchronized methods will not be synchronized to each other. That's why we synchronized on monitor, as there has to be something that those two objects share to make them "hear" each other.
The looping condition should check whether the execution(threads of the current class) need to be temporarily paused.
Taking an example of famous producer-consumer problem, where in the producer will somehow looks like
synchronized(mySharedObj)
{
while(mySharedObj.length==maxSize)
{
mySharedObj.wait();
}
}
If there are n number of Producer Threads on mySharedObj, all will be waiting when the shared resource(mySharedObj) has reached its limit.
I am trying to use 2 threads. 1 thread prints only odd number and the other thread prints only even number and It has to be an alternative operation.
Eg:
Thread1 1
Thread2 2
Thread1 3
Thread2 4
and so on..
Below is the program, please let me know where I am going wrong as the thread1 is not coming out of wait state even when the thread2 is notifying it..
public class ThreadInteraction {
public static void main(String[] args) {
new ThreadInteraction().test();
}
private void test() {
ThreadA ta = new ThreadA();
Thread t = new Thread(ta);
t.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
for(int i=2;i<=50;){
System.out.println("Thread2 "+i);
synchronized (t) {
try {
t.notify();
t.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
i=i+2;
}
}
}
class ThreadA implements Runnable{
#Override
public void run() {
for(int i=1;i<50;){
System.out.println("Thread1 "+i);
synchronized (this) {
try {
notify();
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
i=i+2;
}
}
}
Problem is that in one case you are taking lock on Thread t [synchronized (t) ] while in other case you are taking lock on TheadA object itself [synchronized(this)].
If you want threads to talk to each other then both should take lock on same object only then wait notify will work as you expect.
Edit:
There is another problem in your program, you are not using any variable to coordinate between 2 threads. SO you may see output like this 2,1,4,3...so on. Point is threads will work alternately but not in sequence.
So you should share a single variable between 2 threads which should be incremented.
Second issue is you are not taking care of spurious wake up calls [read some docs on this], you should always have wait called inside a while loop.
Modified my code based on the answer provided by Lokesh
public class ThreadInteraction {
public static void main(String[] args) {
new ThreadInteraction().test();
}
private void test() {
ThreadA ta = new ThreadA();
Thread t = new Thread(ta);
t.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
for(int i=2;i<=50;){
System.out.println("Thread2 "+i);
synchronized (ta) {
try {
ta.notify();
ta.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
i=i+2;
}
}
}
class ThreadA implements Runnable{
#Override
public void run() {
for(int i=1;i<50;){
System.out.println("Thread1 "+i);
synchronized (this) {
try {
notify();
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
i=i+2;
}
}
}
You have a real confusion of threads and locks. I suggest you create one and only one object to use for locking to start with as you don't appear to have a clear idea what you are locking.
If you notify() and nothing is listening, the signal is lost. However, a wait() can wake spuriously.
For this reason, a notify() should be accompanied by a state change and a wait() should be in a loop checking that change.
Code snippet:
class Counter implements Runnable {
Object s = new Object();
#Override
public void run() {
try {
synchronized (s) {
s.wait(10000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
//...do Something
}
public void stopCounter() {
synchronized (s) {
s.notifyAll();
}
}
}
Irrespective of whether i call stopCounter or not, the ...do Something code always executes only after the wait interval. Even after notify it still waits for 10 secs.
I cannot tell from your example what you are trying to achieve. If it is to try and replace some sort of polling then consider the BlockingQueue interface that was released in Java 5. Since that has appeared I have had no need for wait/notify. It's a lot more simple to use and java behind the scenes does the equivalent of the wait/notify for you.
It depends of the way you use it. I have just tried it by adding a main method and running it and it seems like the wait / notify mechanism is working fine, not the way you described it. Please try it yourself:
public static void main(String[] args) {
Counter c = new Counter();
new Thread(c).start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c.stopCounter();
}
My guess is that you call the run and stopCounter methods on different instances of your Counter class. They therefore use different monitors (your s = new Object()) and the call to stop won't notify the other Counter.
For example, this would behave similarly to what you describe (unless you get a spurious wakeup):
public static void main(String[] args) throws InterruptedException {
Counter c = new Counter();
new Thread(c).start();
Thread.sleep(200);
new Counter().stopCounter();
}
static class Counter implements Runnable {
Object s = new Object();
#Override
public void run() {
try {
System.out.println("in");
synchronized (s) {
s.wait(10000);
}
System.out.println("out");
} catch (InterruptedException e) {
e.printStackTrace();
}
//...do Something
}
public void stopCounter() {
synchronized (s) {
s.notifyAll();
}
System.out.println("notified");
}
}
I tried implementing wait/notify mechanism to modify the ArrayList using two separate threads.
It seems to work fine for first iteration but then for second iteration it waits forever in addToArray() method. I am not able to figure out why is it waiting forever in the method? As per my understanding the other thread (removing an item) should pick up when other thread goes to wait.
Please have a look and point out possible bug if any. I know I can use Vector to have thread-safe operation but that is not what I want.
package threadTest;
import java.util.*;
public class DhagaJava {
public static void main(String...strings){
ArrayModification am = new ArrayModification();
Thread t1 = new Thread(new AddToArray(am));
Thread t2 = new Thread(new RemoveFromArray(am));
t1.start();
t2.start();
}
}
class ArrayModification{
boolean added = false;
ArrayList<Integer> al;
ArrayModification(){
al = new ArrayList<Integer>();
}
public synchronized void addToArrayList(int x) {
if (added == true){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.al.add(x);
System.out.println(al);
System.out.println("Added!! :)");
added = true;
notifyAll();
}
public synchronized void removeFromList(){
if( added== false){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(al);
this.al.remove(0);
System.out.println("Removed!! :' ");
added = false;
notifyAll();
}
}
class AddToArray implements Runnable{
ArrayModification ma;
AddToArray(ArrayModification m){
this.ma = m;
}
public void run() {
for (int i = 0; i<10; i++)
ma.addToArrayList(i);
}
}
class RemoveFromArray implements Runnable{
ArrayModification ma;
RemoveFromArray(ArrayModification a){
this.ma = a;
}
public void run(){
ma.removeFromList();
}
}
class RemoveFromArray implements Runnable{
ArrayModification ma;
RemoveFromArray(ArrayModification a){
this.ma = a;
}
public void run(){
//for(int j=11;j<20; j++)
ma.removeFromList();
}
}
Output is:
[0]
Added!! :)
[0]
Removed!! :'
[1]
Added!! :)
Only problem you have is the removeFromList runs only once (because you commended out the for loop). That’s the reason why there is no second remove in the log and that addToArrayList starts waiting forever (waits for someone remove the item from the list).
I tried your code after removing the comment and works fine!
Rather than re-invent the wheel, just use a CopyOnWriteArrayList. It comes with concurrency out of the box.
Your notifyAll is inside the synchronized block. So the other thread may be awaken before he can act. So it may be blocked.
I'm not sure I understood your goal, but this construction could be better :
public void addToArrayList(int x) {
synchonized(this.al) {
if (added == true){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.al.add(x);
System.out.println(al);
System.out.println("Added!! :)");
added = true;
}
notifyAll();
}
But this is very complex. Do you have a more general goal ? Maybe a task queue with only one thread could suit you better : it would be faster, lighter, simpler, and as parallelized (that is not at all).
Consider the following code :-
public class UsingWait1{
public static void main(String... aaa){
CalculateSeries r = new CalculateSeries();
Thread t = new Thread(r);
t.start();
synchronized(r){
try{
r.wait(); //Here I am waiting on an object which is Runnable. So from its run method, it can notify me (from inside a synchronized block).
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
}
System.out.println(r.total);
try{
Thread.sleep(1);
} catch (InterruptedException e){
System.out.println("Interrupted");
}
System.out.println(r.total);
}
}
class CalculateSeries implements Runnable{
int total;
public void run(){
synchronized(this){
for(int i = 1; i <= 10000; i++){
total += i;
}
notify(); // Line 1 .. Notify Exactly one of all the threads waiting on this instance of the class to wake up
}
}
}
Here I am waiting on CalculateSeries which is Runnable. So I can notify the waiting thread from the run() method of CalculateSeries.
But now, consider the following code where I am waiting on an object which is not Runnable.
public class WaitNotOnThread{
public static void main(String... aaa){
NotRunnable nr = new NotRunnable();
IAmRunnable r = new IAmRunnable(nr);
new Thread(r).start();
synchronized(nr){
try{
nr.wait();
} catch(InterruptedException e){
System.out.println("Wait interrupted");
}
System.out.println("After being notified within synchronized");
}
System.out.println("After synchronized");
}
}
class IAmRunnable implements Runnable{
NotRunnable nr;
IAmRunnable(NotRunnable nr){
this.nr = nr;
}
public void run(){
synchronized(nr){
try{
Thread.sleep(1000);
} catch(InterruptedException e){
System.out.println("Sleeping Interrupted :( ");
}
notify(); // Line 2
}
}
}
class NotRunnable{
}
Here I get an IllegalMonitorStateException at Line 2. I am waiting on the same instance of the object (which is not Runnable) while calling both, wait() as well as notify(). Then what is the problem?
Can someone also give some scenarios where it would be useful to wait on an object which is not Runnable??
Wait need not be on Runnable. That is why notify() is on Object and not on Runnable. I guess that helps in all cases we want to avoid busy wait.
The problem seems to be the synchronized() is on nr, and the notify is called on different object. Also synchronized should be on final variables.
class IAmRunnable implements Runnable {
final NotRunnable nr;
IAmRunnable( final NotRunnable nr) {
this.nr = nr;
}
public void run() {
synchronized (nr) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Sleeping Interrupted :( ");
}
nr.notify(); // Line 2
}
}
}