Executes the submitted task. If the maximum number of pooled threads is
in use, this method blocks until one of a them is available.

@param task
@throws InterruptedException
496384
public void execute(final FifoTask<E> task) throws InterruptedException {
        final int id;
        synchronized (this) {
            id = idCounter++;
            taskMap.put(id, task);
            while (activeCounter >= maxThreads) {
                wait();
            }
            activeCounter++;
        }

        this.threadPoolExecutor.execute(new Runnable() {
            public void run() {
                try {
                    try {
                        final E outcome = task.runParallel();
                        synchronized (resultMap) {
                            resultMap.put(id, new Result(outcome));
                        }
                    } catch (Throwable th) {
                        synchronized (resultMap) {
                            resultMap.put(id, new Result(null, th));
                        }
                    } finally {
                        processResults();
                        synchronized (FifoTaskExecutor.this) {
                            activeCounter--;
                            FifoTaskExecutor.this.notifyAll();
                        }
                    }
                } catch (Exception ex) {
                    Logger.getLogger(FifoTaskExecutor.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
                }
            }
        });
    }
228096
public void loop(int count) throws ExecutionException, InterruptedException {
        if (count == -1) {
            while (true) {
                print(context.nextPacket());
            }
        } else {
            for (int i = 0; i < count; i++) {
                print(context.nextPacket());
            }
            executorService.shutdownNow();
        }
    }
284600
public void run() throws Exception {
    try {

      CountDownLatch countDownLatch = new CountDownLatch(this.tasks);
      for (int i = 0; i < this.tasks; i++) {
        addTask(i, countDownLatch);
      }
      // Wait for the tasks to finish
      countDownLatch.await();
    } finally {
      try {
        // Calling close() will stop metric reporting
        this.context.close();
      } finally {
        this.executor.shutdownNow();
      }
    }
  }
696762
public void join(long millis) throws InterruptedException {
    if (!shutdown) {
      throw new IllegalStateException("Cannot wait for active submit thread");
    }
    sched.awaitTermination(millis, TimeUnit.MILLISECONDS);
  }
188817
public void run(Object job) throws InterruptedException
    {
        if(job==null)
            return;
        try
        {
            PoolThread thread=(PoolThread)_pool.get(getMaxIdleTimeMs());
            if(thread!=null)
                thread.run(this,job);
            else
            {
                log.warn("No thread for "+job);
                stopJob(null,job);
            }
        }
        catch(InterruptedException e)
        {
            throw e;
        }
        catch(Exception e)
        {
            log.warn(LogSupport.EXCEPTION,e);
        }
    }
297712
@UnstableApi
    public final void executeAfterEventLoopIteration(Runnable task) {
        ObjectUtil.checkNotNull(task, "task");
        if (isShutdown()) {
            reject();
        }

        if (!tailTasks.offer(task)) {
            reject(task);
        }

        if (wakesUpForTask(task)) {
            wakeup(inEventLoop());
        }
    }
952390
void afterCompletion(Task task) {
        boolean delayable = schedule(task);
        DrainStatus status = drainStatus.get();
        if (status.shouldDrainBuffers(delayable)) {
            tryToDrainBuffers();
        }
        notifyListener();
    }
1137428
private void continueExecution() {
        //increment task index
        i++;
        
        //submit next if the end of the chain not reached yet
        if (i < task.length && task[i] != null) {
        	scheduler.submit(task[i]);            
        } else if (listener != null) {
            listener.onTermination();
        }
    }
383614
public void performAction() throws InterruptedException {
	if (isDone()) {
	    return;
	}

	performing.set(true);

	try {
	    getAction().perform(unschedulable); // Execute action
	} catch (final InterruptedException e) {
	    cancelled.set(true);
	    throw e;
	} catch (final Exception e) { // Continue
	    logger.log(Level.WARNING, "Error performing action", e);
	    if (!isPeriodicOnException()) {
		cancelled.set(true);
	    }
	} finally {
	    performing.set(false);
	    done.set(true);
	    signalRan(); // Wake up awaiting
	}

	if (isCancelled()) {
	    return;
	}

	if (isPeriodic()) {
	    estimated.set(System.nanoTime() + getPeriod(TimeUnit.NANOSECONDS));
	    done.set(false);
	    addAsWork(); // Add to engines work queue
	}
    }
1221767
public void addTask(ReceivedMessageTask task) {
        tasks.add(task);
        Packet packet = task.getPacket();
        // don't run the deadlock guard if timeout is <= 0
        if (packet.getExpirationTime() > 0L) {
            // run a deadlock guard so hanging tasks will be interrupted
            task.runDeadlockFuture(new DeadlockGuard(task));
        }
        if (listener != null) {
            listener.onTaskAdded(this);
        }
    }
962618
public void execute(@Nonnull Runnable task, final ResourceActivity activity ) throws InterruptedException {
        final ResourceList resources = activity.getResourceList();
        _withLock(new NotReallyRoleSensitiveCallable<Void,InterruptedException>() {
            @Override
            public Void call() throws InterruptedException {
                while (inUse.isCollidingWith(resources)) {
                    // TODO revalidate the resource list after re-acquiring lock, for now we just let the build fail
                    _await();
                }

                // we have a go
                inProgress.add(activity);
                inUse = ResourceList.union(inUse, resources);
                return null;
            }
        });

        try {
            task.run();
        } finally {
           // TODO if AsynchronousExecution, do that later
            _withLock(new Runnable() {
                @Override
                public void run() {
                    inProgress.remove(activity);
                    inUse = ResourceList.union(resourceView);
                    _signalAll();
                }
            });
        }
    }
1137514
public static void executeWithSystemPermissions(Runnable task) {
        ContextAwareRunnable contextAwaretask = new ContextAwareRunnable(task);
        Subject newsubject = new Subject.Builder().buildSubject();
        newsubject.login(new RootAuthenticationToken());
        try {
            newsubject.execute(contextAwaretask);
        } finally {
            newsubject.logout();
        }
    }
8797
public void runTask(Handler<Task> task) {
    if (currentTask == null) {
      currentTask = new Task(this);
      task.handle(currentTask);
    } else {
      queue.add(task);
    }
  }
6924
@Override
    public void execute(Runnable task) {
        Executor executor = getScheduledExecutor();
        try {
            executor.execute(errorHandlingTask(task, false));
        } catch (RejectedExecutionException ex) {
            throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
        }
    }
688848
protected synchronized void submitTask(SpiderTask task) {
		if (isStopped()) {
			log.debug("Submitting task skipped (" + task + ") as the Spider process is stopped.");
			return;
		}
		if (isTerminated()) {
			log.debug("Submitting task skipped (" + task + ") as the Spider process is terminated.");
			return;
		}
		this.tasksTotalCount++;
		try {
			this.threadPool.execute(task);
		} catch (RejectedExecutionException e) {
			if (log.isDebugEnabled()) {
				log.debug("Submitted task was rejected (" + task + "), spider state: [stopped=" + isStopped() + ", terminated="
						+ isTerminated() + "].");
			}
		}
	}
677462
public Future<?> submit(Runnable task)
    {
        Preconditions.checkState(isOpen.get(), "CloseableExecutorService is closed");

        InternalFutureTask<Void> futureTask = new InternalFutureTask<Void>(new FutureTask<Void>(task, null));
        executorService.execute(futureTask);
        return futureTask;
    }
584751
void runTasks(Task[] tasks, int maxTaskIndex) {
        for (int i = 0; i <= maxTaskIndex; i++) {
            runTasksInChain(tasks[i]);
            tasks[i] = null;
        }
    }