This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am currently working on a socket based IM server, I get messages from multiple clients simultaneously and put them into queues, for each queue in my server I have a separate listener running in a thread, as soon as a packet comes in the queue (from any client) it�s related thread is notified to get that packet from thread and process it.
Here I am using wait/notify blocking mechanism to control loop in my listener thread. My logic works like this
1)When Listener thread starts it calls the synchronized getNextPacket method on that queue if the queue has any packet (i.e. it�s length is greater then zero) then it will remove that packet from the underlying vector (NOTE: I am using Vector to contain packets) and return that packet to listener thread, else if queue doesn�t have any packet (i.e. its size is zero) then I will call wait() method which causes the listener thread to wait.
2)Now when any client adds some packet in that queue using synchronized addPacket( ) method I first add that packet in the underlying vector and then call notify()/notifyAll() to awake that listener thread, which again calls the getNextPacket() method and this cycle continuous like this.
So multiple threads (clients) are adding data in the queue using synchronized method, only one listener thread is getting data from that queue using a synchronized method again . This approach works fine but sometimes I have noticed that the listener thread doesn�t resume just after notiy() / notifyAll() has been called , sometimes it resumes after a long time sometimes it even don�t resume (after waiting a long time I assumed this).
Solutions I tried 1)I did set the listener Thread�s priority to Maximum but facing same problem again.
For better understanding I am also sending you the code for my queue class and it�s listener thread.
CODE OF QUEUE CLASS
CODE OF LISTENER CLASS
Can anybody please tell me where I am doing something wrong and whats the best way to get rid of this situation . Thanks in advance [ April 22, 2007: Message edited by: sajjad ahmad ]
Looking at your code, I don't see a cause for the behavior your describe. However the getNextCommandPacket() does seem rather strange. It looks like any time it can't find a command, it will wait, and then return null. It is guaranteed to return null after a wait. Why do that? Isn't the purpose of the wait to wait until you can return something besides null? I recommend you put a loop inside the getNextCommandPacket() method, so that the method rechecks if size() > 0 after each wait. Once you have size() > 0, then you can return. And then there's no need to check for null later.
Refactoring the code a bit may make the problem go away. If not, I would recommend adding more logging. In particular add log statements immediately before and after the wait() call, and before and after the notifyAll(). These log statements can also include the name of the thread doing the logging. In log4j you can get this wasily by including %t in the PatternLayout configuration. Or you can print it with Thread.currentThread().getName() if you need to. This will be useful to tell you which thread is doing what.
The objective here is to discover if the listener thread is really in the wait() method when notifyAll() is being sent, or if it's doing something else. If it's doing something else, then you can add more logging statements elsewhere in the program to discover just what he listener is doing when the notifyAll() is sent. That will help increase understanding of what's really happening. Hope that helps...
I suspect by the time you call notify, many of your threads have already returned and are not waiting. So it may look like they are ignoring the call, but its because they were no longer waiting when the call was made.