From: peter r. <pet...@us...> - 2004-05-06 13:46:15
|
Update of /cvsroot/ant-contrib/ant-contrib/src/net/sf/antcontrib/util In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30559/src/net/sf/antcontrib/util Added Files: ExecuteRunnables.java Log Message: <for> implement parallel allow any Path type to be iterated over add fileset and dirset as nested elements --- NEW FILE: ExecuteRunnables.java --- /* * Copyright (c) 2004 Ant-Contrib project. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.antcontrib.util; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.tools.ant.BuildException; /** * A utility class to execute a list * or Runnables in different threads. */ public class ExecuteRunnables { private int maxThreads = 5; private List runnables = new ArrayList(); /** * Add a runnable object to be run. * * @param r a <code>Runnable</code> object. */ public void add(Runnable r) { runnables.add(r); } /** * The maximum number of threads to use. * @param maxThreads the maximum number of threads to use. */ public void setMaxThreads(int maxThreads) { this.maxThreads = maxThreads; } /** * Execute the runnables in the threads. */ public void execute() { ThreadPool pool = new ThreadPool(maxThreads); List threads = new ArrayList(); // start each task in it's own thread, using the // pool to ensure that we don't exceed the maximum // amount of threads for (Iterator i = runnables.iterator(); i.hasNext();) { // get the Runnable object Runnable r = (Runnable) i.next(); // Get a thread, and start the task. // If there is no thread available, this will // block until one becomes available try { ThreadPoolThread tpt = pool.borrowThread(); tpt.setRunnable(r); tpt.start(); threads.add(tpt); } catch (Exception ex) { throw new BuildException(ex); } } // Wait for all threads to finish before we // are allowed to return. for (Iterator i = threads.iterator(); i.hasNext();) { Thread t = (Thread) i.next(); if (t.isAlive()) { try { t.join(); } catch (InterruptedException ex) { throw new BuildException(ex); } } } } } |