[concurrency-interest] Performance improvement using ThreadPoolExecutor

Vishal Byakod vishal.byakod at gmail.com
Mon Apr 27 06:47:49 EDT 2009

I am writing an API( getObject()) which returns a java object from a
cluster. The cluster can have multiple nodes and each node contains
only and only one persistent queue. This API getObject()  needs to
return one object from any one of the queues in the fastest possible
time. The API is a part of a client runtime library which executes on
the client JVM

The client program will call getObject() and block until there is an
object available.
In the getObject() implementation I maintain a ThreadPoolExecutor and
in turn submit a callable thread to fetch queued objects from each of
the cluster nodes. So if we have 3 nodes in the cluster, getObject()
submits 3 callables to the executor. The task of fetching the objects
from each cluster node is completely independent of others,  and for
each node in the cluster I maintain a local in memory queue.

Each callable after fetching the objects from the node, puts the data
in its local queue. I need to return back an object the  moment any of
the local queues are non empty.

I have come up with the foll design, let me know if there areother
ways to improve the performance using any of the concurrency API's
Earlier we were spawning one thread for each cluster node to fetch

Object obj = null;

//API Implementation
protected final Object getObject() throws Exception {
	// check if the local queues for all the nodes are empty.
	if(nonEmptyQueues.size() == 0){
	     for (Iterator nodesList = nodes.iterator(); nodesList.hasNext();){
	     node = (Node)nodesList.next();
	     NodeWorker callable = new NodeWorker();
          return nonEmptyQueues.get(0).receive();

   * Each instance of this class is responsible for getting objects
from a physical queue on one node.
	  private class NodeWorker implements Callable {

	    public Boolean call() {
	          try {
	            // wait for objects to be fetched
	            // this call retrives more than 30 objects if available from the
	            // cluster node
	            boolean gotMessages = this.fetchMessages(timeout);
	          }catch (Exception e){
	          return gotMessages;

More information about the Concurrency-interest mailing list