I made the following producer consumer problem with blocking queue with capacity 1. So that producer can produce only one item but running the code producer can produce 2 items and consumer can consume even though queue is empty.
please help in solving this weird behavior as per me this wrong behavior. Producer should block if queue is empty.
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ArrayBlockingQueue;
class prodconsimpl{
public static void main(String[] args){
BlockingQueue<Integer> arr=new ArrayBlockingQueue<Integer>(1);
producer pd=new producer(arr);
consumer cs=new consumer(arr);
Thread t1=new Thread(pd);
Thread t2=new Thread(cs);
t1.start();
t2.start();
}
}
class producer implements Runnable{
BlockingQueue<Integer> arr;
producer(BlockingQueue<Integer> arr){
this.arr=arr;
}
public void run(){
for(int i=1;i<=4;i++){
try{
System.out.println(Thread.currentThread().getName() +"producing "+i+" "+arr);
arr.put(i);
// System.out.println(Thread.currentThread().getName() +" "+arr);
System.out.println(Thread.currentThread().getName() +"produced "+i+" "+arr);
}
catch(InterruptedException ex){
ex.printStackTrace();
}
}
}
}
class consumer implements Runnable{
BlockingQueue<Integer> arr;
consumer(BlockingQueue<Integer> arr){
this.arr=arr;
}
public void run(){
while(true){
int i=0;;
try{
System.out.println(Thread.currentThread().getName() +"consuming "+" "+ arr);
//System.out.println(Thread.currentThread().getName() +" "+arr);
i=arr.take();
//System.out.println(Thread.currentThread().getName() +" "+arr);
System.out.println(Thread.currentThread().getName() +"consumed " + i+" "+arr);
}
catch(InterruptedException ex){
ex.printStackTrace();
}
if(i==4)
break;
}
}
}
On running the code it gives below output
Thread-1consuming []
Thread-0producing 1 []
Thread-0produced 1 [1]
Thread-1consumed 1 []
Thread-0producing 2 []
Thread-1consuming []
Thread-1consumed 2 []
Thread-0produced 2 [2]
Thread-1consuming []
Thread-0producing 3 []
Thread-0produced 3 [3]
Thread-0producing 4 []
Thread-0produced 4 [4]
Thread-1consumed 3 []
Thread-1consuming [4]
Thread-1consumed 4 []