public static void main(String[] args) {
   BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(12);
   ThreadFactory threadFactory =
       new ThreadFactory() {
         public Thread newThread(Runnable r) {
           int currentCount = counter.getAndIncrement();
           System.out.println("Creating new thread: " + currentCount);
           return new Thread(r, "mythread" + currentCount);
         }
       };
   RejectedExecutionHandler rejectedHandler =
       new RejectedExecutionHandler() {
         public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
           if (r instanceof ThreadPoolExecutorExample) {
             ThreadPoolExecutorExample example = (ThreadPoolExecutorExample) r;
             System.out.println("Rejecting task with id " + example.getTaskId());
           }
         }
       };
   ThreadPoolExecutor executor =
       new ThreadPoolExecutor(5, 12, 1, TimeUnit.SECONDS, queue, threadFactory, rejectedHandler);
   for (int i = 0; i < 100; i++) {
     executor.execute(new ThreadPoolExecutorExample(i));
   }
   executor.shutdown();
 }
  static void oneRun(BlockingQueue<Runnable> q, int nThreads, int iters, boolean print)
      throws Exception {

    ThreadPoolExecutor pool =
        new ThreadPoolExecutor(nThreads + 1, Integer.MAX_VALUE, 1L, TimeUnit.SECONDS, q);

    CountDownLatch done = new CountDownLatch(iters);
    remaining.set(nThreads - 1);
    pool.prestartAllCoreThreads();
    Task t = new Task(pool, done);
    long start = System.nanoTime();
    pool.execute(t);
    done.await();
    long time = System.nanoTime() - start;
    if (print) System.out.println("\t: " + LoopHelpers.rightJustify(time / iters) + " ns per task");
    q.clear();
    Thread.sleep(100);
    pool.shutdown();
    Thread.sleep(100);
    pool.shutdownNow();
  }
Ejemplo n.º 3
0
  @Override
  public synchronized void close() throws SecurityException {
    if (pool.isShutdown()) {
      return;
    }

    try {
      // first, anything in the retry queue should be tried one last time and then we give up on it
      allowRetry = false;
      for (LogglySample sample : retryQueue) {
        pool.submit(sample);
      }
      retryQueue.clear();

      System.out.println(
          "Shutting down Loggly handler - waiting 90 seconds for "
              + pool.getQueue().size()
              + " logs to finish");
      pool.shutdown();
      try {
        boolean result = pool.awaitTermination(90, TimeUnit.SECONDS);
        if (!result) {
          System.out.println(
              "Not all Loggly messages sent out - still had "
                  + pool.getQueue().size()
                  + " left :(");
          pool.shutdownNow();
        }
      } catch (InterruptedException e) {
        // ignore
      }
    } finally {
      httpClient.getConnectionManager().shutdown();
      System.out.println("Loggly handler shut down");
    }
  }
Ejemplo n.º 4
0
  @Override
  public void stop() {

    if (sharedHttpServers != null) {
      for (HttpServer server : sharedHttpServers.values()) {
        server.close();
      }
      sharedHttpServers = null;
    }

    if (sharedNetServers != null) {
      for (NetServer server : sharedNetServers.values()) {
        server.close();
      }
      sharedNetServers = null;
    }

    if (timer != null) {
      timer.stop();
      timer = null;
    }

    if (eventBus != null) {
      eventBus.close(null);
    }

    if (backgroundPool != null) {
      backgroundPool.shutdown();
    }

    if (acceptorPool != null) {
      acceptorPool.shutdown();
    }

    try {
      if (backgroundPool != null) {
        backgroundPool.awaitTermination(20, TimeUnit.SECONDS);
        backgroundPool = null;
      }
    } catch (InterruptedException ex) {
      // ignore
    }

    try {
      if (acceptorPool != null) {
        acceptorPool.awaitTermination(20, TimeUnit.SECONDS);
        acceptorPool = null;
      }
    } catch (InterruptedException ex) {
      // ignore
    }

    // log.info("Release external resources from worker pool");
    if (corePool != null) {
      corePool.releaseExternalResources();
      corePool = null;
    }
    // log.info("Release external resources: done");

    setContext(null);
  }
 /**
  * Initiates an orderly shutdown in which previously submitted tasks are executed, but no new
  * tasks will be accepted. Invocation has no additional effect if already shut down.
  *
  * <p>This method does not wait for previously submitted tasks to complete execution. Use {@link
  * #awaitTermination awaitTermination} to do that.
  *
  * <p>If the {@code ExecuteExistingDelayedTasksAfterShutdownPolicy} has been set {@code false},
  * existing delayed tasks whose delays have not yet elapsed are cancelled. And unless the {@code
  * ContinueExistingPeriodicTasksAfterShutdownPolicy} has been set {@code true}, future executions
  * of existing periodic tasks will be cancelled.
  */
 public void shutdown() {
   super.shutdown();
 }