org/objectweb/proactive/core/component/collectiveitfs/GatherFuturesHandlerPool.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.component.collectiveitfs;
00032 
00033 import java.util.ArrayList;
00034 import java.util.Enumeration;
00035 import java.util.Hashtable;
00036 
00037 import org.objectweb.proactive.ActiveObjectCreationException;
00038 import org.objectweb.proactive.ProActive;
00039 import org.objectweb.proactive.core.node.NodeException;
00040 
00048 public class GatherFuturesHandlerPool {
00049     static int created = 0;
00050     static int reused = 0;
00051     static int passivated = 0;
00052     static int terminated = 0;
00053     public ArrayList<GatherFuturesHandler> pool;
00054     private static GatherFuturesHandlerPool instance = null;
00055 
00056     public static GatherFuturesHandlerPool instance() {
00057         if (instance == null) {
00058             instance = new GatherFuturesHandlerPool();
00059         }
00060         return instance;
00061     }
00062 
00063     private GatherFuturesHandlerPool() {
00064         expirationTime = 10000;
00065         locked = new Hashtable<GatherFuturesHandler, Long>();
00066         unlocked = new Hashtable<GatherFuturesHandler, Long>();
00067     }
00068 
00069     private long expirationTime;
00070     private Hashtable<GatherFuturesHandler, Long> locked;
00071     private Hashtable<GatherFuturesHandler, Long> unlocked;
00072 
00073     GatherFuturesHandler create()
00074         throws ActiveObjectCreationException, NodeException {
00075         //                      System.out.println("CREATED " + ++created );
00076         return (GatherFuturesHandler) ProActive.newActive(GatherFuturesHandler.class.getName(),
00077             new Object[] {  });
00078     }
00079 
00080     boolean validate(GatherFuturesHandler handler) {
00081         return true;
00082     }
00083 
00084     void expire(GatherFuturesHandler handler) {
00085         ProActive.terminateActiveObject(handler, false);
00086     }
00087 
00088     public synchronized GatherFuturesHandler borrowFuturesHandler()
00089         throws ActiveObjectCreationException, NodeException {
00090         long now = System.currentTimeMillis();
00091         GatherFuturesHandler handler;
00092         if (unlocked.size() > 0) {
00093             Enumeration<GatherFuturesHandler> e = unlocked.keys();
00094             while (e.hasMoreElements()) {
00095                 handler = (GatherFuturesHandler) e.nextElement();
00096                 if ((now - ((Long) unlocked.get(handler)).longValue()) > expirationTime) {
00097                     // object has expired
00098                     unlocked.remove(handler);
00099                     expire(handler);
00100                     handler = null;
00101                 } else {
00102                     if (validate(handler)) {
00103                         unlocked.remove(handler);
00104                         locked.put(handler, new Long(now));
00105 //                        System.out.println("REUSED " + ++reused);
00106                         return (handler);
00107                     } else {
00108                         // object failed validation
00109                         unlocked.remove(handler);
00110                         expire(handler);
00111                         handler = null;
00112                     }
00113                 }
00114             }
00115         }
00116         // no objects available, create a new one
00117         handler = create();
00118         locked.put(handler, new Long(now));
00119         return (handler);
00120     }
00121 
00122     public synchronized void returnFuturesHandler(GatherFuturesHandler handler) {
00123         locked.remove(handler);
00124         handler.passivate();
00125         unlocked.put(handler, new Long(System.currentTimeMillis()));
00126     }
00127 
00128 }

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