org/objectweb/proactive/core/body/ft/servers/util/ActiveQueue.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.body.ft.servers.util;
00032 
00033 import java.util.Hashtable;
00034 
00035 
00041 public class ActiveQueue extends Thread {
00042     private java.util.ArrayList<ActiveQueueJob> queue;
00043     private int counter;
00044     private boolean kill;
00045     private Hashtable<ActiveQueueJob, JobBarrier> barriers;
00046 
00047     public ActiveQueue(String name) {
00048         queue = new java.util.ArrayList<ActiveQueueJob>();
00049         counter = 0;
00050         kill = false;
00051         barriers = new Hashtable<ActiveQueueJob, JobBarrier>();
00052         this.setName(name);
00053     }
00054 
00055     //
00056     // -- PUBLIC METHODS -----------------------------------------------
00057     //
00058 
00063     public java.util.ArrayList<ActiveQueueJob> getQueue() {
00064         return queue;
00065     }
00066 
00071     public synchronized void addJob(ActiveQueueJob j) {
00072         queue.add(j);
00073         counter++;
00074         notifyAll();
00075     }
00076 
00083     public synchronized JobBarrier addJobWithBarrier(ActiveQueueJob j) {
00084         JobBarrier b = new JobBarrier();
00085         this.barriers.put(j, b); // hash method of job !!!
00086         queue.add(j);
00087         counter++;
00088         notifyAll();
00089         return b;
00090     }
00091 
00096     public synchronized ActiveQueueJob removeJob() {
00097         counter--;
00098         return (queue.remove(0));
00099     }
00100 
00104     public synchronized void killMe() {
00105         kill = true;
00106         notifyAll();
00107     }
00108 
00113     public void run() {
00114         while (true) {
00115             // if there is no job to do, wait...
00116             waitForJob();
00117             // if someone want to kill me, break the loop
00118             if (kill) {
00119                 break;
00120             }
00121 
00122             // there are jobs to do !           
00123             ActiveQueueJob toDo = this.removeJob();
00124             if (toDo != null) {
00125                 toDo.doTheJob();
00126                 // unlock barrier if any
00127                 JobBarrier b = (this.barriers.get(toDo));
00128                 if (b != null) {
00129                     // this job is barriered
00130                     b.signalJobCompletion();
00131                     this.barriers.remove(toDo);
00132                 }
00133             }
00134         }
00135     }
00136 
00137     // synchronized wait on job queue
00138     private synchronized void waitForJob() {
00139         try {
00140             while ((counter == 0) && !kill) {
00141                 wait();
00142             }
00143         } catch (InterruptedException e) {
00144             e.printStackTrace();
00145         }
00146     }
00147 }

Generated on Mon Jan 22 15:16:05 2007 for ProActive by  doxygen 1.5.1