1

I've written the following code, but I feel I'm going wrong somewhere:

public class ProcessQueue { static BlockingQueue<String> queue = new LinkedBlockingQueue<String>(); public ProcessQueue() { process(); } public void add(String message) throws InterruptedException { System.out.println("Added Queue size:" + queue.size()); System.out.println("Locked by Producer"); queue.put(message); System.out.println("Lock Released by Producer"); } public static void process() { new Thread() { @Override public void run() { try { while (true) { System.out.println("Locked by Consumer"); Message.send(queue.take()); System.out.println("Locked Released by Consumer"); System.out.println("Consuming Queue size:" + queue.size()); } } catch (Exception ex) { System.out.print(ex.getMessage()); } } }.start(); } } 

Here add(String) adds the string to the queue. It is called whenever it receives an input from a UDP port. process() processes the queue and sends it for processing to the class Message. The output Locked and Released Print Statements is not in the desired order.

EDIT

My expected answer should be: if it's in Producer that is add then Locked by Producer -> then add to Queue -> Lock Release. same way would be in consumer. But the operations shouldn't interleave i.e. once locked by producer is printed it shouldn't print locked by consumer and then release locks.

3
  • what are the expected and actual orders? Commented Apr 13, 2012 at 18:00
  • whats your desired order and inwhich order its printing?. Commented Apr 13, 2012 at 18:01
  • @mfrankli please see the EDIT section Commented Apr 14, 2012 at 3:58

1 Answer 1

3

The only time blocking will occur here is on take when the queue is empty. Otherwise puts will continue to happen. So you may see the queue's size not increment by one. You may want to put a bound on the LinkedBlockingQueue. Fyi the LBQ is default unbounded

Edit based on your edit:

My answer thus far is explaining what you are seeing and why. You are looking for a synchronous messaging passing queue. You can do this with the following:

new SynchrnousQueue(); new LinkedBlockingQueue(1); new ArrayBlockingQueue(1); new TransferQueue(); 

SynchrnousQueue does exactly what you want. The Linked&ArrayBlockingQueue with a bound of 1 pretty much does the same. TransferQueue is a new queue offered in Java 7 which has transfer methods that wait until a thread is ready to acquire.

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

5 Comments

does this mean that the above code for a producer consumer problem is correct?
Yes, for a single-producer single-consumer
okay. that would solve my current issue. What should I do so that it works with multiple producers or consumers?
You can do a new ArrayBlockingQueue(1).
Well assuming you want only one thread to be in that critical section at a time, the 1 says the queue has a depth of 1. Meaning only one object can be in the queue at any given time. So that would force only one thread to be able to pull from the queue and put into the queue at any given time

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.