1

I'm writing Code for a Network Application. Therefor I'm using a LinkedBlockingQueue to store incoming messaged until they are consumed. The following code runs in it's own Thread and fills up the Queue:

while(true) {
    String msg = in.readLine();
    if(msg == null) continue;
    recieveQueue.offer(msg);
    System.out.println("recieveQueue.offer() called at: " + System.currentTimeMillis() + "    hash:" + recieveQueue.hashCode());
    System.out.println("Server recieved: " + msg.replace("\n", ""));
    break;
}

Next I wrote a Method, which runs in the same "Main-Thread" (No extra Thread is created for this Method). It's only called when the stored Elements have to be consumed. It looks like the following:

public String recieveMessage() {
    try {
        System.out.println("recieveQueue.take() called at: " + System.currentTimeMillis() + "    hash:" + recieveQueue.hashCode());
        return recieveQueue.take();
    }catch(InterruptedException e) {
        e.printStackTrace();
        return null;
    }
}

When running this Code I get the following output:

recieveQueue.offer() called at: 1594558123030    hash:2091496189
Server recieved: CONFIRMED
recieveQueue.take() called at: 1594558123031    hash:2091496189

The hash verifies that I'm working on the same Queue, and as seen by the Time, recieveQueue.offer(msg) is definitely called before take() and so the Queue should contain a message. But the Program stops at the take() call. No Exceptions were thrown and there is no other point in the code where take() gets called. I waited for like 10 minutes but the take() call never finishes

1 Answers1

0

Made few changes in your program.

Note: Please check in your code Consumer in while loop.

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

public class Main
{
    public static void main(String[] args) {
    BlockingQueue recieveQueue = new LinkedBlockingQueue<String>(10);
    
    new Thread(){
       public void run(){
       int count=0;
          while(true) {
                    count++;
                    String msg = "AAA:"+count;
                    if(msg == null) continue;
                    recieveQueue.offer(msg);
                    System.out.println("recieveQueue.offer() called at: " + System.currentTimeMillis() + "    hash:" + recieveQueue.hashCode());
                    System.out.println("Server recieved: " + msg.replace("\n", ""));
          }
       } 
    }.start();
    
    while(true){
        try {
            System.out.println("recieveQueue.take() called at: " + System.currentTimeMillis() + "    hash:" + recieveQueue.hashCode());
            System.out.println("recieveQueue.take() : "+recieveQueue.take());
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    
 }
}
  • The break is on purpose. This while loop is wrapped in another while loop and after one message is read, the next message should be send. So this is not the problem –  Jul 12 '20 at 16:06
  • Ok, As posted code outer while loop you missed to provide. Please check the updated working code, you can simulate with your code logic. – Hareesh M D Jul 12 '20 at 16:53