0
class Producer implements Runnable{  //producer job here
    Vector<Integer> sQueue;
    public Producer(Vector<Integer> queue) {
        this.sQueue=queue;
    }
    public void run(){
        int i=0;
        try{
            synchronized(sQueue){
                for(i=0;i<30;i++){
                    if(sQueue.size()<15){
                        System.out.println("Producer producing the item:-"+i);
                        sQueue.add(i);
                        sQueue.notifyAll();
                    }
                    else
                    {   sQueue.notifyAll();
                        sQueue.wait();

                    }
                }
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

}
class Consumer implements Runnable{
    Vector<Integer> sQueue;
    public Consumer(Vector<Integer> queue) {
        this.sQueue=queue;
    }
    public void run(){
        int i=0;
        try{
            synchronized(sQueue){
                while(true){
                    if(sQueue.size()>0){
                        System.out.println("Consumer is removing the item:-");
                        int item=sQueue.remove(0);
                        System.out.println(item);
                        sQueue.notifyAll();
                        Thread.sleep(1000);
                    }
                    else
                    {
                        sQueue.wait();
                    }
                }
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}
public class ProducerConsumerProblem {
    public static void main(String [] arg){
        Vector<Integer> sQueue;
        sQueue = new Vector<>(15);
        Producer p=new Producer(sQueue);
        Consumer c=new Consumer(sQueue);
        Thread producer=new Thread(p);
        Thread consumer=new Thread(c);
        producer.start();
        consumer.start();
    }
}

In the above code, i am trying to simulate producer consumer problem with the help of java multi threading. I am using sQueue as a shared queue which is of integer i dont know what is going wrong , producer thread and consumer thread are not interleaving, here is the output on my machine

----------------------------output------------------------
Producer producing the item:-0
Producer producing the item:-1
Producer producing the item:-2
Producer producing the item:-3
Producer producing the item:-4
Producer producing the item:-5
Producer producing the item:-6
Producer producing the item:-7
Producer producing the item:-8
Producer producing the item:-9
Producer producing the item:-10
Producer producing the item:-11
Producer producing the item:-12
Producer producing the item:-13
Producer producing the item:-14
Consumer is removing the item:-
0
Consumer is removing the item:-
1
Consumer is removing the item:-
2
Consumer is removing the item:-
3
Consumer is removing the item:-
4
Consumer is removing the item:-
5
Consumer is removing the item:-
6
Consumer is removing the item:-
7
Consumer is removing the item:-
8
Consumer is removing the item:-
9
Consumer is removing the item:-
10
Consumer is removing the item:-
11
Consumer is removing the item:-
12
Consumer is removing the item:-
13
Consumer is removing the item:-
14
Producer producing the item:-16
Producer producing the item:-17
Producer producing the item:-18
Producer producing the item:-19
Producer producing the item:-20
Producer producing the item:-21
Producer producing the item:-22
Producer producing the item:-23
Producer producing the item:-24
Producer producing the item:-25
Producer producing the item:-26
Producer producing the item:-27
Producer producing the item:-28
Producer producing the item:-29
Consumer is removing the item:-
16
Consumer is removing the item:-
17
Consumer is removing the item:-
18
Consumer is removing the item:-
19
Consumer is removing the item:-
20
Consumer is removing the item:-
21
Consumer is removing the item:-
22
Consumer is removing the item:-
23
Consumer is removing the item:-
24
Consumer is removing the item:-
25
Consumer is removing the item:-
26
Consumer is removing the item:-
27
Consumer is removing the item:-
28
Consumer is removing the item:-
29
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • What do you expect exactly? it is not clear – Nicolas Filotto Aug 18 '16 at 16:29
  • I expect interleaving of producer and consumer threads, in above output it is observed that until consumer thread or producer thread is blocked by wait() method, they are not interleaving even after calling notifyAll() – saksham jain Aug 18 '16 at 17:46

1 Answers1

1

Your methods are synchronized. When the Producer starts, it obtains a lock on the queue, and doesn't let go of it until it completes. When your Consumer starts, it waits until it can obtain the lock. If you want them to interleave, don't synchronize... the synchronized keyword is specifically to prevent interleaving.

Edward Peters
  • 3,623
  • 2
  • 16
  • 39
  • but i am using notifyAll() to release the lock inside synchronized block isnt it? – saksham jain Aug 18 '16 at 17:44
  • @sakshamjain `notifyAll()` doesn't release the lock: It's `wait()` that does that. But look at your output: Your producer thread loops exactly fifteen times before the consumer gets to run. Fifteen also is how many items you allow the producer to put in the queue before the producer calls `wait()`. – Solomon Slow Aug 18 '16 at 17:49
  • @sakshamjain So, I'm no expert and could be wrong about this, but from the Java Docs on notifyAll: "The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object.". I believe that means that it doesn't release the lock, it just tells them to check in on it - so they wake up, check that it's still locked, and then continue to wait. – Edward Peters Aug 18 '16 at 17:49
  • @EdwardPeters yes thats right i narrowed the scope of synchronized block and it worked thanks – saksham jain Aug 18 '16 at 18:19