I have the following code:
Queue<Message> localMsgQueue;
//inside the constructor
localMsgQueue = new ConcurrentLinkedQueue<Message>();
public void AddMsg(Message m)
{
localMsgQueue.offer(m);
System.out.println("^^"+localMsgQueue.size());
}
public void GetMsg() {
System.out.println("+++" + localMsgQueue.size());
}
Here, the AddMsg is called inside the main thread that initializes the queue. The GetMsg is called from a different thread, from time to time.
My problem is size check inside AddMsg returns the correct number of elements in the queue, while GetMsg always returns ZERO size, although the queue contains elements.
Can someone point out what the problem here? This is the first time I am using Java concurrent queue, so not sure what is going on.
You are probably constructing two different
ConcurrentLinkedQueue<Message>objects.Put a breakpoint inside the constructor of your class and see how many times it gets hit.