FileDocCategorySizeDatePackage
ActiveRequestQueue.javaAPI DocExample3247Mon Feb 28 17:58:14 GMT 2000tuning.threads

ActiveRequestQueue

public class ActiveRequestQueue extends PassiveRequestQueue implements Runnable

Fields Summary
int
MAXIMUM_NUM_SERVERS
ActiveRequestQueue[]
servers
RequestProcessor
requestProcessor
Thread
myThread
ActiveRequestQueue
queueServer
boolean
isAvailable
Constructors Summary
private ActiveRequestQueue(ActiveRequestQueue q)



  //Internal queue object - processes requests
    
  
    queueServer = q;
    requestProcessor=new RequestProcessor();
  
public ActiveRequestQueue(int num_servers)

    //Create a pool of queue servers and start them in their own threads
    servers = new ActiveRequestQueue[num_servers];
    Thread t;
    for (int i = servers.length-1; i>=0 ; i--)
    {
      servers[i] = new ActiveRequestQueue(this);
      (t = new Thread(servers[i])).start();
      servers[i].myThread = t;
    }
  
Methods Summary
public synchronized voidacceptRequest(Request r)

    //Override the super class accept to increase the number
    //of servers if they are all busy

    //If we already have the maximum number of threads,
    //just queue the request
    if (servers.length >= MAXIMUM_NUM_SERVERS)
    {
        super.acceptRequest(r);
        return;
    }

    //otherwise, if one of the servers is available, just queue
    //the request
    for (int i = servers.length-1; i>=0 ; i--)
    {
      if (servers[i].isAvailable())
      {
        super.acceptRequest(r);
        return;
      }
    }

    //otherwise, increase the server pool by one, then queue the request
    Thread t;
    ActiveRequestQueue[] tmp_servers = servers;
    servers = new ActiveRequestQueue[tmp_servers.length+1];
    System.arraycopy(tmp_servers, 0, servers, 0, tmp_servers.length);
    servers[tmp_servers.length] = new ActiveRequestQueue(this);
    (t = new Thread(servers[tmp_servers.length])).start();
    servers[tmp_servers.length].myThread = t;
    super.acceptRequest(r);
  
public booleanisAvailable()

 return isAvailable;
public voidreturnResult(RequestResult r)

public voidrun()

    Request request;
    RequestResult result;

    //Private queues use this method.

    //Basically, we just ask the public server for a request.
    //The releaseRequest() method blocks until one is available.
    //Then we process it and start again.
    for(;;)
    {
      request = queueServer.releaseRequest();
      //Terminate the thread when it gets a null request
      if (request == null)
        return;
      isAvailable = false;
      result = requestProcessor.processRequest(request);
      returnResult(result);
      isAvailable = true;
    }