@Override
 public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
   if (r instanceof AbstractRunnable) {
     if (((AbstractRunnable) r).isForceExecution()) {
       BlockingQueue<Runnable> queue = executor.getQueue();
       if (!(queue instanceof SizeBlockingQueue)) {
         throw new IllegalStateException("forced execution, but expected a size queue");
       }
       try {
         ((SizeBlockingQueue) queue).forcePut(r);
       } catch (InterruptedException e) {
         Thread.currentThread().interrupt();
         throw new IllegalStateException("forced execution, but got interrupted", e);
       }
       return;
     }
   }
   rejected.inc();
   StringBuilder sb = new StringBuilder("rejected execution ");
   if (executor.isShutdown()) {
     sb.append(SHUTTING_DOWN_KEY + " ");
   } else {
     if (executor.getQueue() instanceof SizeBlockingQueue) {
       sb.append("(queue capacity ")
           .append(((SizeBlockingQueue) executor.getQueue()).capacity())
           .append(") ");
     }
   }
   sb.append("on ").append(r.toString());
   throw new EsRejectedExecutionException(sb.toString());
 }
 public boolean force(Runnable o, long timeout, TimeUnit unit) throws InterruptedException {
   if (parent.isShutdown())
     throw new RejectedExecutionException(
         "Executor not running, can't force a command into the queue");
   return super.offer(
       o, timeout, unit); // forces the item onto the queue, to be used if the task is rejected
 }
 public void cancel(Runnable runnable) {
   if (threadPoolExecutor != null
       && !threadPoolExecutor.isShutdown()
       && !threadPoolExecutor.isTerminated()) {
     threadPoolExecutor.remove(runnable);
   }
 }
Exemple #4
0
 @Override
 public void rejectedExecution(Runnable r, ThreadPoolExecutor pool) {
   throw new ThreadPoolRejectedExecutionException(
       r,
       pool,
       pool.getQueue().remainingCapacity() == 0
           ? "No more space in the work queue!"
           : pool.isShutdown() ? "The pool is not running!" : "reason unknown!");
 }
    /** {@inheritDoc} */
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
      try {
        if (executor.isShutdown()) throw new RejectedExecutionException();
        else executor.getQueue().put(r);
      } catch (InterruptedException ignored) {
        U.warn(log, "Working thread was interrupted while loading data.");

        Thread.currentThread().interrupt();
      }
    }
  /** 主函数 */
  public static void main(String[] args) {

    final ThreadPoolExecutor pool = ApplyThreadPool.getThreadPoolExector();

    Runtime.getRuntime()
        .addShutdownHook(
            new Thread(
                new Runnable() {
                  @Override
                  public void run() {
                    pool.shutdown();
                  }
                }));

    /** 从150机器上获取皮皮用户uid和token */
    logger.info("从150机器上获取皮皮用户uid: ");
    TokenService tokenService = new TokenService();

    /** 连接48数据库 */
    logger.info("连接48数据库: ");
    WeiboJDBC weiboJDBC =
        new WeiboJDBC("192.168.1.48", "pp_fenxi", "q#tmuYzC@sqB6!ok@sHd", "pp_fenxi");

    // 创建粉丝分析结果数据表
    try {
      weiboJDBC.createFansAnalysisTable(FansAnalysisInfoUtils.PP_SINA_FANS_ANALYSIS);
    } catch (SQLException e) {
      throw new RuntimeException(e);
    }
    weiboJDBC.dbClose();

    /** 循环计算 */
    logger.info("循环计算: ");
    for (Long uid : tokenService.getSinaUids()) {
      if (!pool.isShutdown()) {
        pool.execute(new FansAnalysisRun(uid));
      }
    }

    pool.shutdown();
    try {
      pool.awaitTermination(100, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
 public void rejectedExecution(Runnable task, ThreadPoolExecutor executor) {
   ((DebuggableThreadPoolExecutor) executor).onInitialRejection(task);
   BlockingQueue<Runnable> queue = executor.getQueue();
   while (true) {
     if (executor.isShutdown()) {
       ((DebuggableThreadPoolExecutor) executor).onFinalRejection(task);
       throw new RejectedExecutionException("ThreadPoolExecutor has shut down");
     }
     try {
       if (queue.offer(task, 1000, TimeUnit.MILLISECONDS)) {
         ((DebuggableThreadPoolExecutor) executor).onFinalAccept(task);
         break;
       }
     } catch (InterruptedException e) {
       throw new AssertionError(e);
     }
   }
 }
    @Override
    public void rejectedExecution(Runnable task, ThreadPoolExecutor executor) {

      BlockingQueue<Runnable> queue = executor.getQueue();

      while (true) {
        if (executor.isShutdown()) {
          throw new RejectedExecutionException("ThreadPoolExecutor has shut down");
        }

        try {
          if (queue.offer(task, 300, TimeUnit.MILLISECONDS)) {
            break;
          }
        } catch (InterruptedException e) {
          throw Throwables.propagate(e);
        }
      }
    }
 @Test
 public void testShutdownWithReferencesDoesNotStopExecutor() throws Exception {
   Map<String, Object> cache = new HashMap<String, Object>();
   ThreadPoolBuilder builder =
       new ThreadPoolBuilder(name.getTableNameString(), new Configuration(false));
   ThreadPoolExecutor exec = ThreadPoolManager.getExecutor(builder, cache);
   assertNotNull("Got a null exector from the pool!", exec);
   ThreadPoolExecutor exec2 = ThreadPoolManager.getExecutor(builder, cache);
   assertTrue("Should have gotten the same executor", exec2 == exec);
   exec.shutdown();
   assertFalse(
       "Executor is shutting down, even though we have a live reference!",
       exec.isShutdown() || exec.isTerminating());
   exec2.shutdown();
   // wait 5 minutes for thread pool to shutdown
   assertTrue(
       "Executor is NOT shutting down, after releasing live reference!",
       exec.awaitTermination(300, TimeUnit.SECONDS));
 }
  public void testShutdownNowInterrupts() throws Exception {
    String threadPoolName = randomThreadPool(ThreadPool.ThreadPoolType.FIXED);
    ThreadPool threadPool = null;
    try {
      Settings nodeSettings =
          Settings.builder()
              .put("threadpool." + threadPoolName + ".queue_size", 1000)
              .put("node.name", "testShutdownNowInterrupts")
              .build();
      threadPool = new ThreadPool(nodeSettings);
      ClusterSettings clusterSettings =
          new ClusterSettings(nodeSettings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
      threadPool.setClusterSettings(clusterSettings);
      assertEquals(info(threadPool, threadPoolName).getQueueSize().getSingles(), 1000L);

      final CountDownLatch latch = new CountDownLatch(1);
      ThreadPoolExecutor oldExecutor = (ThreadPoolExecutor) threadPool.executor(threadPoolName);
      threadPool
          .executor(threadPoolName)
          .execute(
              () -> {
                try {
                  new CountDownLatch(1).await();
                } catch (InterruptedException ex) {
                  latch.countDown();
                  Thread.currentThread().interrupt();
                }
              });
      clusterSettings.applySettings(
          Settings.builder().put("threadpool." + threadPoolName + ".queue_size", 2000).build());
      assertThat(threadPool.executor(threadPoolName), not(sameInstance(oldExecutor)));
      assertThat(oldExecutor.isShutdown(), equalTo(true));
      assertThat(oldExecutor.isTerminating(), equalTo(true));
      assertThat(oldExecutor.isTerminated(), equalTo(false));
      threadPool.shutdownNow(); // should interrupt the thread
      latch.await(
          3, TimeUnit.SECONDS); // If this throws then ThreadPool#shutdownNow didn't interrupt
    } finally {
      terminateThreadPoolIfNeeded(threadPool);
    }
  }
  public void processMTRoot(String path) {
    long t0 = System.currentTimeMillis();
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    try {
      ts.addIndexPath(new File(path).getCanonicalPath());
      System.out.println("MediaIndexer.processMTRoot()" + path);
      System.out.println("MediaIndexer.processMTRoot() started at time " + dateFormat.format(date));
      System.out.println("MediaIndexer.processMTRoot() computing number of files...");
      totalNumberOfFiles = this.countFiles(path);
      lastProgressTime = System.currentTimeMillis();
      System.out.println("Number of files to explore " + totalNumberOfFiles);
      if (executorService.isShutdown()) {
        executorService =
            new ThreadPoolExecutor(
                maxThreads, maxThreads, 0L, TimeUnit.MILLISECONDS, new LimitedQueue<Runnable>(50));
      }

      this.processedFiles = 0;
      // this.processMT(new File(path));
      TreeWalker t = new TreeWalker(this);
      t.walk(path);
    } catch (IOException e) {
      e.printStackTrace();
    }
    executorService.shutdown();
    try {
      executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    long t1 = System.currentTimeMillis();
    date = new Date();
    System.out.println("MediaIndexer.processMTRoot() finished at time " + dateFormat.format(date));
    System.out.println("MediaIndexer.processMTRoot() found " + newFiles + " new files");
    System.out.println("MediaIndexer.processMTRoot() updated " + updatedFiles + " files");
    System.out.println("MediaIndexer.processMTRoot() total " + ts.size() + " files");
    System.out.println("MediaIndexer.processMTRoot took " + (t1 - t0) / 1000 + " s");
  }
Exemple #12
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");
    }
  }
 protected void doStart() throws Exception {
   if (executor.isTerminated() || executor.isTerminating() || executor.isShutdown())
     throw new IllegalStateException("Cannot restart");
 }
 public boolean force(Runnable o) {
   if (parent.isShutdown())
     throw new RejectedExecutionException(
         "Executor not running, can't force a command into the queue");
   return super.offer(o); // forces the item onto the queue, to be used if the task is rejected
 }
 public boolean isShutdown() {
   return pool.isShutdown();
 }
 public boolean isShutdown() {
   return executorService_.isShutdown();
 }