/** * Executes a task on behalf of this thread pool. If all threads are currently * busy, this method call blocks until a thread gets idle again which is when * the call returns immediately. * @param task * a <code>Runnable</code> to execute. */ public synchronized void execute(WorkerTask task) { while (true) { for (int i=0; i<taskManagers.size(); i++) { TaskManager tm = (TaskManager) taskManagers.get(i); if ((respawnThreads) && (!tm.isAlive())) { tm = new TaskManager(getTaskManagerName(name, i)); } if (tm.isIdle()) { tm.execute(task); return; } } try { wait(); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } }
/** * Interrupts all threads in the pool. * @since 1.6 */ public synchronized void interrupt() { for (int i=0; i<taskManagers.size(); i++) { TaskManager tm = (TaskManager) taskManagers.get(i); tm.interrupt(); } }
/** * Stops all threads in this thread pool gracefully. This method will not * return until all threads have been terminated and joined successfully. */ public void stop() { List tms; synchronized (this) { stop = true; tms = (List) taskManagers.clone(); } for (int i=0; i<tms.size(); i++) { TaskManager tm = (TaskManager) tms.get(i); tm.terminate(); synchronized (tm) { tm.notify(); } try { tm.join(); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } }
for (int i=0; i<taskManagers.size(); i++) { TaskManager tm = taskManagers.get(i); if ((respawnThreads) && (!tm.isAlive())) { tm = new TaskManager(getTaskManagerName(name, i)); taskManagers.set(i, tm); if (tm.isIdle()) { synchronized (this) { if (tm.isIdle()) { try { tm.execute(task); return; } catch (IllegalStateException isex) { if (tm.isIdle()) { waitForNotify = false; break;
for (int i = 0; i < taskManagers.size(); i++) { TaskManager tm = taskManagers.get(i); if ((respawnThreads) && (!tm.isAlive())) { tm = new TaskManager(getTaskManagerName(name, i)); taskManagers.set(i, tm); if (tm.isIdle()) { synchronized (this) { if (tm.isIdle()) { try { tm.execute(task); return; } catch (IllegalStateException isex) { if (tm.isIdle()) { waitForNotify = false; break;
/** * Tries to execute a task on behalf of this thread pool. If all threads are * currently busy, this method returns <code>false</code>. Otherwise the task * is executed in background. * @param task * a <code>Runnable</code> to execute. * @return * <code>true</code> if the task is executing. * @since 1.6 */ public boolean tryToExecute(WorkerTask task) { for (int i=0; i<taskManagers.size(); i++) { TaskManager tm = taskManagers.get(i); if ((respawnThreads) && (!tm.isAlive())) { tm = new TaskManager(getTaskManagerName(name, i)); } if (tm.isIdle()) { try { tm.execute(task); return true; } catch (IllegalStateException isex) { // ignore } } } return false; }
/** * Stops all threads in this thread pool gracefully. This method will not * return until all threads have been terminated and joined successfully. */ @SuppressWarnings("unchecked") public void stop() { List<? extends TaskManager> tms; synchronized (this) { stop = true; tms = (List<? extends TaskManager>) taskManagers.clone(); } for (TaskManager tm : tms) { tm.terminate(); synchronized (tm) { tm.notify(); } try { tm.join(); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } }
/** * Tries to execute a task on behalf of this thread pool. If all threads are * currently busy, this method returns <code>false</code>. Otherwise the task * is executed in background. * * @param task * a <code>Runnable</code> to execute. * * @return <code>true</code> if the task is executing. * @since 1.6 */ public boolean tryToExecute(WorkerTask task) { for (int i = 0; i < taskManagers.size(); i++) { TaskManager tm = taskManagers.get(i); if ((respawnThreads) && (!tm.isAlive())) { tm = new TaskManager(getTaskManagerName(name, i)); } if (tm.isIdle()) { try { tm.execute(task); return true; } catch (IllegalStateException isex) { // ignore } } } return false; }
/** * Stops all threads in this thread pool gracefully. This method will not * return until all threads have been terminated and joined successfully. */ @SuppressWarnings("unchecked") public void stop() { List<? extends TaskManager> tms; synchronized (this) { stop = true; tms = taskManagers; } for (TaskManager tm : tms) { tm.terminate(); synchronized (tm) { tm.notify(); } try { tm.join(); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } }
/** * Tries to execute a task on behalf of this thread pool. If all threads are * currently busy, this method returns <code>false</code>. Otherwise the task * is executed in background. * @param task * a <code>Runnable</code> to execute. * @return * <code>true</code> if the task is executing. * @since 1.6 */ public synchronized boolean tryToExecute(WorkerTask task) { for (int i=0; i<taskManagers.size(); i++) { TaskManager tm = (TaskManager) taskManagers.get(i); if ((respawnThreads) && (!tm.isAlive())) { tm = new TaskManager(getTaskManagerName(name, i)); } if (tm.isIdle()) { tm.execute(task); return true; } } return false; }
/** * Checks if all threads of the pool are idle. * @return * <code>true</code> if all threads are idle. * @since 1.6 */ public boolean isIdle() { for (TaskManager tm : taskManagers) { if (!tm.isIdle()) { return false; } } return true; }
/** * Checks if all threads of the pool are idle. * @return * <code>true</code> if all threads are idle. * @since 1.6 */ public synchronized boolean isIdle() { for (int i=0; i<taskManagers.size(); i++) { TaskManager tm = (TaskManager) taskManagers.get(i); if (!tm.isIdle()) { return false; } } return true; }
/** * Checks if all threads of the pool are idle. * * @return <code>true</code> if all threads are idle. * @since 1.6 */ public boolean isIdle() { for (TaskManager tm : taskManagers) { if (!tm.isIdle()) { return false; } } return true; }
/** * Interrupts all threads in the pool. * @since 1.6 */ public void interrupt() { for (TaskManager tm : taskManagers) { tm.interrupt(); } }
protected void setup(String name, int size) { this.name = name; taskManagers = new Vector(size); for (int i=0; i<size; i++) { TaskManager tm = new TaskManager(getTaskManagerName(name, i)); taskManagers.add(tm); tm.start(); } }
/** * Interrupts all threads in the pool. * * @since 1.6 */ public void interrupt() { for (TaskManager tm : taskManagers) { tm.interrupt(); } }
protected void setup(String name, int size) { this.name = name; taskManagers = new Vector<TaskManager>(size); for (int i=0; i<size; i++) { TaskManager tm = new TaskManager(getTaskManagerName(name, i)); taskManagers.add(tm); tm.start(); } }