Why notify/notifyAll() doesnt awake a thread in waiting mode???
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sajjad_ahmad
Posted On:   Sunday, April 22, 2007 01:05 PM

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 retur   More>>


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 dont 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
import java.util.Vector;
import org.apache.log4j.Logger;
import com.tcm.unicorn.server.UnicornCustomeObject;

/**
* @author sajjad.paracha
*
*/
public class UIMPCommandsQueue {

private static final Logger logger=Logger.getLogger(UIMPCommandsQueue.class);

/**
* Contains all the packets from clients
*/
private Vector unicornCustomeObjectQueue = new Vector ();
private UIMPCommandsQueue(){
}
private static UIMPCommandsQueue uIMPCommandsQueue = null;

public static UIMPCommandsQueue getInstance(){
synchronized(UIMPCommandsQueue.class){
if(uIMPCommandsQueue!=null){
return uIMPCommandsQueue;
}else
return uIMPCommandsQueue = new UIMPCommandsQueue();

}
}

/**
* Adds a new command
* @param unicornCustomeObject
*/
public synchronized void addCommandPakcet(UnicornCustomeObject unicornCustomeObject){
logger.debug("[[[[[[[[[[[[[[[[[[[[[[[[[[ Going to add a packet in queue no "+unicornCustomeObject.getClientSession().getRequestQueueNo());
unicornCustomeObjectQueue.add(unicornCustomeObject);
//** Notify the Listener (RequestProcessor) Thread that a new packet has been arrived in the queue
//** So it now can again start it's processing
notifyAll();
}
/**
* Removes an object from queue whose processing has been started or completed
* @param unicornCustomeObject
* @return
*/
private boolean removeCommandPacket(UnicornCustomeObject unicornCustomeObject){
return unicornCustomeObjectQueue.remove(unicornCustomeObject);
}
/**
*

If no packet is available in queue it retuns null value
* otherwise returns an object from that queue
*


* @return unicornCustomeObject
*/
public synchronized UnicornCustomeObject getNextCommandPacket(){
if(unicornCustomeObjectQueue.size()>0){

UnicornCustomeObject unicornCustomeObject = unicornCustomeObjectQueue.get(0);
logger.debug("[[[[[[[[[[[[[[[[[[[[[[[[[[ Got a packet from queue no "+unicornCustomeObject.getClientSession().getRequestQueueNo());
logger.debug("[[[[[[[[[[[[[[[[[[[[[[[[[[ Going to remove a packet from queue no "+unicornCustomeObject.getClientSession().getRequestQueueNo());
removeCommandPacket(unicornCustomeObject);
return unicornCustomeObject;
}else{
try {
//** Force the Listener (RequestProcessor) Thread to wait for notification
//** This Thread will be only notified if a new command packet has been arrived(added) in the
//** Queue i.e in addCommandPacket Method
wait();
} catch (InterruptedException e) {

logger.error("",e);
}
return null;
}
}
}



			
CODE OF LISTENER CLASS
import org.apache.log4j.Logger;
import com.tcm.unicorn.server.UnicornCustomeObject;

public class RequestProcessor implements Runnable {


/**
* will listen on Request queue for any new massages
*/
public void run() {
//** get an instance of RequestQueue before the loop
UIMPCommandsQueue requestQueue= UIMPCommandsQueue.getInstance();
while(true){
try{
//**call the blocking method getNextCommandPacket()
UnicornCustomeObject unicornCustomeObject= requestQueue.getNextCommandPacket();
if(unicornCustomeObject!=null){
System.out.println("Got a pcket will process it now.......");
}
}catch(Exception exp){
exp.printStackTrace();
}
}
}
}


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

   <<Less

Re: Why notify/notifyAll() doesnt awake a thread in waiting mode???

Posted By:   Edward_Harned  
Posted On:   Monday, April 23, 2007 12:35 PM

This is the third forum in which you've posted this question. You obviously have no respect whatsoever for those who give their time to help others.
About | Sitemap | Contact