Search code examples
javalinked-listpolling

Event based model for polling a message from a linked list


I have a linked list on which messages are stored. I would like to 'listen' to the linked-list and would want to process the message serially (one at a time , at its own pace). The point is, I do not want to poll for the messages in a infinite 'while' loop, unless you tell me it is fine. I think that is a in-efficient way of doing things. So basically I need a queue which behaves like a blocking queue at one end (receiver) i.e the linked list listener gets up only when message is posted on it. Is there a way to achieve it?


Solution

  • Try this:

    LinkedBlockingQueue<String> blockingQueue = new LinkedBlockingQueue<>();
    blockingQueue.take()
    

    You will need a different thread on the other side to take the messages and handle them