private static void Pause(String description) throws Exception { Singletons.Log.Debug( "Pausing for " + Settings.PAUSE_SECONDS + " seconds: " + description + ". Other threads may be processing these tasks."); MiscUtilities.Sleep(Settings.PAUSE_SECONDS * 1000); Singletons.Log.Debug("Done with pause: " + description + "."); }
/** * Executes a series of tasks that are "locked" such that only one thread should execute each. * These are locked across multiple compute nodes as well. * * @param description Name of the series of tasks that will be executed * @param lockedCallables Tasks that will be executed * @throws Exception */ public static void ExecuteLockTasks(String description, ArrayList<LockedCallable> lockedCallables) throws Exception { MultiThreadedTaskHandler taskHandler = new MultiThreadedTaskHandler(description); // Prepare the tasks to be executed for (LockedCallable callable : lockedCallables) { FileUtilities.CreateFileDirectoryIfNotExists(callable.StatusFilePath); taskHandler.Add(callable); } try { // See if any of the tasks returned a false value and if so, pause then retry if (ListUtilities.AnyFalse(ListUtilities.CreateBooleanList(taskHandler.Execute()))) { Pause(description); ExecuteLockTasks(description, lockedCallables); } } catch (Exception ex) { // If an exception occurred, log it, pause, then try again Singletons.Log.Exception(ex); Pause(description); ExecuteLockTasks(description, lockedCallables); } }
/** * Executes multiple callable objects in a multithreaded fashion * * @return Objects that are returned from each _callable object * @throws Exception */ public ArrayList Execute() throws Exception { if (_callables == null || _callables.size() == 0) return new ArrayList(); Singletons.Log.Debug( "Attempting to share execution across " + _numThreads + " threads for " + _description + "."); // Initialize the service // ExecutorService service = Executors.newFixedThreadPool(_numThreads); ExecutorService service = new TimeoutThreadPoolExecutor(_numThreads); try { ArrayList<Future<Object>> futures = new ArrayList<Future<Object>>(); // Submit each task to a queue to be executed for (Callable<Object> callable : _callables) futures.add(service.submit(callable)); ArrayList results = new ArrayList(); // Parse through the results of the execution for (Future<Object> future : futures) { Object result = future.get(); if (result != null) results.add(result); } return results; } catch (Exception ex) { throw ex; } finally { // Very important to shut down the service service.shutdown(); } }