org/objectweb/proactive/calcium/proactive/ProActiveThreadedManager.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.calcium.proactive;
00032 
00033 import java.util.concurrent.Callable;
00034 import java.util.concurrent.ExecutorService;
00035 import java.util.concurrent.Executors;
00036 
00037 import org.objectweb.proactive.ProActive;
00038 import org.objectweb.proactive.calcium.Interpreter;
00039 import org.objectweb.proactive.calcium.Skernel;
00040 import org.objectweb.proactive.calcium.Task;
00041 import org.objectweb.proactive.core.descriptor.data.VirtualNode;
00042 import org.objectweb.proactive.core.node.Node;
00043 
00044 public class ProActiveThreadedManager extends AbstractProActiveManager {
00045 
00046         ExecutorService threadPool;
00047 
00048         public ProActiveThreadedManager(Node nodes[]){          
00049                 super(nodes);
00050         }
00051         
00052         public ProActiveThreadedManager(VirtualNode vn){
00053                 super(vn);
00054         }
00055         
00056         public ProActiveThreadedManager(String descriptorPath, String virtualNodeName){
00057                 super(descriptorPath,virtualNodeName);
00058         }
00059 
00060         @Override
00061         public Skernel boot(Skernel skernel) {
00062                 logger.info("ProActive skeleton manager is using "+nodes.length+" nodes");
00063                 threadPool=Executors.newCachedThreadPool();
00064                 try {
00065                         for(int i=0;i<nodes.length;i++){
00066                                 Interpreter interp=(Interpreter)ProActive.newActive(Interpreter.class.getName(),null,nodes[i]);
00067                                 threadPool.submit(new ProActiveCallableInterpreter(skernel,interp));
00068                         }
00069                 } catch (Exception e) {
00070                         logger.error("Error, unable to create interpreter active objects");
00071                         e.printStackTrace();
00072                 }
00073                 return skernel;
00074         }
00075         
00076         @Override
00077         public void shutdown() {
00078                 super.shutdown();
00079                 if(threadPool != null){
00080                         threadPool.shutdownNow();
00081                 }
00082         }
00083         
00084         protected class ProActiveCallableInterpreter implements Callable<Task<?>>{
00085                         
00086                 Interpreter interp;
00087                 Skernel skernel;
00088                 
00089                 public ProActiveCallableInterpreter(Skernel skernel, Interpreter interp){
00090                         this.skernel=skernel;
00091                         this.interp=interp;
00092                 }
00093 
00094                 public Task<?> call() throws Exception {
00095                         
00096                         Task<?> task = skernel.getReadyTask(0);
00097                         
00098                         while(task!=null){
00099                                 task = interp.interpret(task);
00100                                 
00101                                 ProActive.waitFor(task); //Wait for the future
00102 
00103                                 skernel.putTask(task);
00104 
00105                                 task = skernel.getReadyTask(0);
00106                         }
00107 
00108                         return task;
00109                 }
00110         }
00111 }

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