0

Problem statement : There are n Thread , n/2 thread are producer and n/2 are consumer, number produced by producer-1 thread must be consumed by consumer-1 thread. Thread must also run in order, producer 1, then consumer 1, again producer 2 and then consumer 2…so on…..

I am implementing producer consumer in java using thread but requirement is that there are N/2 producer thread and N/2 Consumer Thread, N consumer thread should consume value produce by N producer and n-1 consumer thread should consume by n-1 producer value.

I have implementing this using blocking queue but not getting the desire output :

package paytm;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ProducerConsumerWithBlockingQueue {
public static class Producer implements Runnable {
    private BlockingQueue<Integer> queue;
    private int next = 0;
    private String thereadName;
    public Producer(BlockingQueue<Integer> queue,String threadName) {
            this.queue = queue;
            this.thereadName = threadName;
    }

    @Override
    public void run() {
            while (true) {
                    try {
                            if(next<10) {
                            queue.put(next);
                            System.out.println(thereadName+ " "+ next);
                            }


                    } catch (InterruptedException e) {
                    }
                    next++;
            }
    }
 }
public static class Consumer implements Runnable {
    private BlockingQueue<Integer> queue;
    private String thereadName;
    public Consumer(BlockingQueue<Integer> queue,String threadName) {
            this.queue = queue;
            this.thereadName = threadName;
    }

    @Override
    public void run() {
            while (true) {
                    synchronized (queue) {
                            Integer next;
                            try {
                                    next = queue.take();
                                    System.out.println(thereadName+ " "+ next);
                            } catch (InterruptedException e) {
                            }
                    }
            }
     }
}

 public static void main(String args[]) throws Exception {
    BlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>(1);
    Thread producer1 = new Thread(new Producer(queue,"producer1"));
    Thread producer2 = new Thread(new Producer(queue,"producer2"));
    Thread consumer1 = new Thread(new Consumer(queue,"Consumer1"));
    Thread consumer2 = new Thread(new Consumer(queue,"Consumer2"));
    producer1.start();
    producer2.start();
    consumer1.start();
    consumer2.start();

//        producer1.join();
//        producer2.join();
//        consumer1.join();
//        consumer2.join();
}
}

// Output :
 producer1 0
 consumer1 0
 producer2 1
 consumer2 1
 producer3 2
 consumer3 2   so on...
6
  • 3
    What is desire output? Commented Sep 6, 2018 at 11:08
  • There are n Thread , n/2 thread are producer and n/2 are consumer, number produced by producer-1 thread must be consumed by consumer-1 thread. Thread must also run in order, producer 1, then consumer 1, again producer 2 and then consumer 2…so on….. Commented Sep 6, 2018 at 11:10
  • Then just change your code to use separate blocking queue per pair producer - consumer. This will guarantee that consumer3 will receive message from producer3 only. Commented Sep 6, 2018 at 11:13
  • 1
    Because you're using threads and threads run in parallel you can't ensure that you get your desired output without having all the threads wait on each other and effectively run in series (negating the benefit of threads). I think you should try to define the problem you want to solve instead because based on the example output you've provided I think you're looking for the wrong solution :) Commented Sep 6, 2018 at 11:30
  • Re, "...Thread must also run in order..." That is like teaching a horse to dance. You can do it, and you might learn something about horses in the process, but there are other, far more useful things that horses can be taught to do. Practically every useful thing you can do with threads is grounded in the idea that threads are able to run in no particular order. Commented Sep 6, 2018 at 13:35

1 Answer 1

1

This might not do what you expect it to do:

while (true)
    synchronized(queue) {
        ...
    }
}

Let's suppose that consumer1 wins the race and gets in to the synchronized block while consumer2 is forced to wait. What do you suppose will happen when consumer1 does its thing, and then exits from the synchronized block?

The Java Language Specification does not say what must happen in that case, but in most implementations, what actually will happen is, consumer1 will go right back into the synchronized block before consumer2 even starts to wake up.

Intrinsic locks in Java are not fair.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.