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 ]