@Override
  public void dispose() {
    if (!hasAlreadyShutdown.getAndSet(true) && executorService != null) {

      for (final Future<?> job : jobs) {
        if (!job.isCancelled() && !job.isDone()) {
          job.cancel(true);
        }
      }

      executorService.shutdown(); // Disable new tasks from being submitted
      try {
        // Wait a while for existing tasks to terminate
        if (!executorService.awaitTermination(AWAIT_TERMINATION_TIMEOUT, TimeUnit.SECONDS)) {
          executorService.shutdownNow(); // Cancel currently executing tasks
          // Wait a while for tasks to respond to being cancelled
          if (!executorService.awaitTermination(AWAIT_TERMINATION_TIMEOUT, TimeUnit.SECONDS)) {
            LOG.error("Thread pool did not terminate.");
          }
        }
      } catch (InterruptedException ie) {
        // (Re-)Cancel if current thread also interrupted
        executorService.shutdownNow();
        // Preserve interrupt status
        Thread.currentThread().interrupt();
      }

      executorService.shutdown();
    }
  }
  @Test
  public void testDeadLockWithNotify() throws Throwable {
    int size = 10;
    final ExecutorService es =
        new ThreadPoolExecutor(
            size * 2, Integer.MAX_VALUE, 30, TimeUnit.SECONDS, new SynchronousQueue());
    ((ThreadPoolExecutor) es).prestartAllCoreThreads();

    for (int i = 0; i < size; i++)
      es.execute(new WriteSpaceWithNotifyTask("WriteTask1-" + i, sp1, sp2));
    for (int i = 0; i < size; i++)
      es.execute(new WriteSpaceWithNotifyTask("WriteTask2-" + i, sp2, sp1));

    long stamp = System.currentTimeMillis();
    while (((ThreadPoolExecutor) es).getActiveCount() > 0) {
      if (System.currentTimeMillis() - stamp < 10000) {
        ISOUtil.sleep(100);
        continue;
      }
      es.shutdownNow();
      fail("Probably death-lock detected");
      return;
    }

    printAvg(t1, "Avg. write: ");

    //        es.shutdown();
    es.shutdownNow();
    es.awaitTermination(5, TimeUnit.SECONDS);
  }
Beispiel #3
0
 void cancelDiscovery1() {
   doLog("cancelDiscovery1()...");
   if (state == eState.idle) {
     doLog("cancelDiscovery1: abort as state!=idle");
     return;
   }
   doLog("cancelDiscovery1: stutdown ExecutorService");
   es.shutdown();
   try {
     doLog("cancelDiscovery1: waiting for es.shutdown...");
     // Wait a while for existing tasks to terminate
     if (!es.awaitTermination(60, TimeUnit.SECONDS)) {
       es.shutdownNow(); // Cancel currently executing tasks
       // Wait a while for tasks to respond to being cancelled
       if (!es.awaitTermination(60, TimeUnit.SECONDS))
         System.err.println("Pool did not terminate");
     }
     doLog("cancelDiscovery1: es terminated");
   } catch (InterruptedException ie) {
     // (Re-)Cancel if current thread also interrupted
     doLog("cancelDiscovery1: trying es.shutdownNow...");
     es.shutdownNow();
     // Preserve interrupt status
     doLog("cancelDiscovery1: Preserve interrupt status");
     Thread.currentThread().interrupt();
   }
   doLog("cancelDiscovery1: set state=finished");
   //        state=eState.finished;
   Message msg = mHandler.obtainMessage(msgTypes.finished);
   Bundle bundle = new Bundle();
   msg.setData(bundle);
   mHandler.sendMessage(msg);
   state = eState.idle;
   doLog("cancelDiscovery1: END");
 }
 @Override
 public void doTraverse(Poset gPoset) {
   DefaultPoset poset = (DefaultPoset) gPoset;
   @SuppressWarnings("unchecked")
   final Queue<VectorClock> ques[] = new Queue[2];
   for (int i = 0; i < 2; ++i) ques[i] = new ConcurrentLinkedQueue<VectorClock>();
   ques[0].add(new VectorClock(poset.width(), 0));
   final AtomicInteger curQue = new AtomicInteger(0);
   final CyclicBarrier barrier = new CyclicBarrier(poolSize, new Tripper(ques, curQue));
   final ExecutorService pool = Executors.newFixedThreadPool(poolSize);
   for (int i = 0; i < poolSize; ++i) {
     pool.submit(new Worker(poset, ques, curQue, barrier, enumerator.fork()));
   }
   pool.shutdown();
   try {
     if (!pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS)) {
       pool.shutdownNow();
       if (!pool.awaitTermination(1, TimeUnit.SECONDS))
         logger.error("Thread pool did not terminate.");
     }
   } catch (InterruptedException e) {
     pool.shutdownNow();
     logger.warn("Thread pool is forced to shutdown.");
   }
 }
  /** @param pool */
  private void shutdownAndAwaitTermination(final ExecutorService pool) {

    if (serverSocket != null) {

      try {
        serverSocket.close();
      } catch (IOException e) {
        LOG.debug("IO Exception closing server socket", e);
      }
    }

    pool.shutdown(); // Disable new tasks from being submitted

    try {
      // Cancel currently executing tasks
      pool.shutdownNow();

      // Wait a while for tasks to respond to being cancelled
      if (!pool.awaitTermination(5, TimeUnit.SECONDS)) {
        LOG.warn("Federation data channel pool did not terminate");
      }

    } catch (InterruptedException ie) {
      // (Re-)Cancel if current thread also interrupted
      pool.shutdownNow();
    }
  }
  protected synchronized void initializeQueue(String host, String queueName, Integer port)
      throws InterruptedException {
    final String bind = "tcp://" + host + ":" + port;
    _log.warn("binding to " + bind);
    if (_context == null) {
      _context = ZMQ.context(1);
    }
    if (_socket != null) {
      _executorService.shutdownNow();
      _heartbeatService.shutdownNow();
      Thread.sleep(1 * 1000);
      _log.warn("_executorService.isTerminated=" + _executorService.isTerminated());
      _socket.close();
      _executorService = Executors.newFixedThreadPool(1);
      _heartbeatService = Executors.newFixedThreadPool(1);
    }

    _socket = _context.socket(ZMQ.PUB);
    _socket.connect(bind);
    _executorService.execute(new SendThread(_socket, queueName));
    _heartbeatService.execute(new HeartbeatThread(HEARTBEAT_INTERVAL));

    _log.debug("Inference output queue is sending to " + bind);
    _initialized = true;
  }
Beispiel #7
0
 public final void a() {
   try {
     cm.a()
         .b()
         .a(
             "Crashlytics",
             (new StringBuilder("Executing shutdown hook for ")).append(a).toString());
     b.shutdown();
     if (!b.awaitTermination(c, d)) {
       cm.a()
           .b()
           .a(
               "Crashlytics",
               (new StringBuilder())
                   .append(a)
                   .append(
                       " did not shut down in the allocated time. Requesting immediate shutdown.")
                   .toString());
       b.shutdownNow();
     }
     return;
   } catch (InterruptedException interruptedexception) {
     cm.a()
         .b()
         .a(
             "Crashlytics",
             String.format(
                 Locale.US,
                 "Interrupted while waiting for %s to shut down. Requesting immediate shutdown.",
                 new Object[] {a}));
   }
   b.shutdownNow();
 }
  public static void shutdown() {
    logger_.info("Shutting down ...");
    synchronized (MessagingService.class) {
      /* Stop listening on any socket */
      for (SelectionKey skey : listenSockets_.values()) {
        SelectorManager.getSelectorManager().cancel(skey);
      }
      listenSockets_.clear();

      /* Shutdown the threads in the EventQueue's */
      messageDeserializationExecutor_.shutdownNow();
      messageSerializerExecutor_.shutdownNow();
      messageDeserializerExecutor_.shutdownNow();
      streamExecutor_.shutdownNow();

      /* shut down the cachetables */
      taskCompletionMap_.shutdown();
      callbackMap_.shutdown();

      /* Interrupt the selector manager thread */
      SelectorManager.getSelectorManager().interrupt();

      poolTable_.clear();
      verbHandlers_.clear();
      bShutdown_ = true;
    }
    logger_.debug("Shutdown invocation complete.");
  }
Beispiel #9
0
 private void shutdownExecutor(ExecutorService executorService) {
   executorService.shutdown();
   try {
     if (!executorService.awaitTermination(1, TimeUnit.MINUTES)) {
       executorService.shutdownNow();
     }
   } catch (InterruptedException e) {
     executorService.shutdownNow();
   }
 }
Beispiel #10
0
  @After
  public void cleanup() throws InterruptedException {
    // env1.shutdown();
    // env2.shutdown();

    clientPool.shutdownNow();
    clientPool.awaitTermination(5, TimeUnit.SECONDS);

    serverPool.shutdownNow();
    serverPool.awaitTermination(5, TimeUnit.SECONDS);
  }
  /**
   * Stops engine, cancels all running and scheduled display image tasks. Clears internal data. <br>
   * <b>NOTE:</b> This method doesn't shutdown {@linkplain
   * com.nostra13.universalimageloader.core.ImageLoaderConfiguration.Builder#taskExecutor(java.util.concurrent.Executor)
   * custom task executors} if you set them.
   */
  void stop() {
    if (!configuration.customExecutor) {
      ((ExecutorService) taskExecutor).shutdownNow();
    }
    if (!configuration.customExecutorForCachedImages) {
      ((ExecutorService) taskExecutorForCachedImages).shutdownNow();
    }

    cacheKeysForImageAwares.clear();
    uriLocks.clear();
  }
 protected void stopThreads() {
   if (!mInitialized) {
     throw new IllegalArgumentException("Threads already shutdown.");
   }
   mSendExecutor.shutdown();
   mReceiveExecutor.shutdownNow();
   mExecutor.shutdownNow();
   mSendExecutor = null;
   mReceiveExecutor = null;
   mExecutor = null;
   mInitialized = false;
 }
Beispiel #13
0
  public static void destroy() {
    EXECUTOR.shutdown();

    try {
      if (!EXECUTOR.awaitTermination(1, TimeUnit.SECONDS)) {
        EXECUTOR.shutdownNow();
      }
    } catch (Exception e) {
      EXECUTOR.shutdownNow();

      Thread.currentThread().interrupt();
    }
  }
 /** 停止任务 */
 private void stopTask() {
   if (mExecutorService != null) {
     mExecutorService.shutdown();
     try {
       if (!mExecutorService.awaitTermination(60, TimeUnit.SECONDS)) {
         mExecutorService.shutdownNow();
         if (!mExecutorService.awaitTermination(60, TimeUnit.SECONDS)) {}
       }
     } catch (InterruptedException ie) {
       mExecutorService.shutdownNow();
     }
   }
 }
 private void shutdownGracefully(final ExecutorService pool) {
   pool.shutdown();
   try {
     if (pool.awaitTermination(120, TimeUnit.SECONDS)) return;
     pool.shutdownNow();
     if (pool.awaitTermination(60, TimeUnit.SECONDS)) return;
     log.error("Importer pool did not terminate within timeout.");
     System.exit(1);
   } catch (InterruptedException e) {
     pool.shutdownNow();
     Thread.currentThread().interrupt();
   }
 }
 public void unInit() {
   if (mExecutorService != null) {
     mExecutorService.shutdown();
     mExecutorService.shutdownNow();
     mExecutorService = null;
   }
 }
Beispiel #17
0
 public static void shutdown() throws InterruptedException {
   excutor.shutdownNow();
   while (!excutor.awaitTermination(5, TimeUnit.SECONDS)) {
     log.info("await for keyManager shutdown!");
   }
   log.info("keyManager has shutdown!");
 }
  public void dispose() {
    fireApplicationExiting();
    disposeComponents();

    ourThreadExecutorsService.shutdownNow();
    super.dispose();
  }
  /**
   * Create N VMs using the given cloud provider
   *
   * @param N
   * @param cp
   * @return an ordered list with the times necessary to create each VM. If a VM is not created, the
   *     size of the returned list is less then N.
   */
  public List<Long> createVMs(int N, CloudProviderType cpType) {

    CloudProvider cp = CloudProviderFactory.getInstance(cpType);
    ExecutorService executor = Executors.newFixedThreadPool(N);
    List<Future<Long>> futures = new ArrayList<Future<Long>>();
    for (int i = 0; i < N; i++) {
      Future<Long> future = executor.submit(new VMCreator(cp));
      futures.add(future);
      awsOneRequestPerSecondRuleSleep(); // EC2 1 second rule
    }

    Concurrency.waitExecutor(executor, TIMEOUT);
    System.out.println("");

    List<Long> times = new ArrayList<Long>();
    for (Future<Long> f : futures) {
      Long time = this.checkFuture(f);
      if (time != null) {
        times.add(time);
      }
    }

    executor.shutdownNow();
    Collections.sort(times);
    return times;
  }
Beispiel #20
0
  @Test
  public void test10TimesRemoveMessageListener() throws InterruptedException {
    ExecutorService ex = Executors.newFixedThreadPool(1);
    final CountDownLatch latch = new CountDownLatch(10);

    try {
      ex.execute(
          new Runnable() {
            public void run() {
              for (int i = 0; i < 10; i++) {
                try {
                  removeMessageListener();
                  latch.countDown();
                } catch (InterruptedException e) {
                  return;
                }
              }
            }
          });

      assertTrue(latch.await(30, TimeUnit.SECONDS));
    } finally {
      ex.shutdownNow();
    }
  }
Beispiel #21
0
  @Override
  public void onDestroy() {
    super.onDestroy();

    Log.i(TAG, "Stopping service");

    stopForeground(true);

    if (acceptor != null) {
      try {
        acceptor.close();
      } catch (IOException e) {
        // We don't care
      }
    }

    try {
      executor.shutdownNow();
      executor.awaitTermination(10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
      // Too bad
    } finally {
      started = false;

      // Unfortunately, we have no way to clean up some Binder-based callbacks
      // (namely IRotationWatcher) on lower API levels without killing the process,
      // which will allow DeathRecipient to handle it on their side.
      Process.killProcess(Process.myPid());
    }
  }
Beispiel #22
0
  private static boolean getResultWithTimeout(
      Callable<Boolean> startBloombergProcess, int timeout, TimeUnit timeUnit) {
    ExecutorService executor =
        Executors.newSingleThreadExecutor(
            new ThreadFactory() {
              @Override
              public Thread newThread(Runnable r) {
                Thread t = new Thread(r, "Bloomberg - bbcomm starter thread");
                t.setDaemon(true);
                return t;
              }
            });
    Future<Boolean> future = executor.submit(startBloombergProcess);

    try {
      return future.get(timeout, timeUnit);
    } catch (InterruptedException ignore) {
      Thread.currentThread().interrupt();
      return false;
    } catch (ExecutionException | TimeoutException e) {
      logger.error("Could not start bbcomm", e);
      return false;
    } finally {
      executor.shutdownNow();
      try {
        if (!executor.awaitTermination(100, TimeUnit.MILLISECONDS)) {
          logger.warn("bbcomm starter thread still running");
        }
      } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
      }
    }
  }
 private void doShutdownExecutorService(ExecutorService executorService) {
   if (this.shutdownForced) {
     executorService.shutdownNow();
   } else {
     executorService.shutdown();
   }
 }
Beispiel #24
0
  private Pair<Map<String, String>, /*was it created now?*/ Boolean> getOrCreateIndexConfig(
      Class<? extends PropertyContainer> cls,
      String indexName,
      Map<String, String> suppliedConfig) {
    Pair<Map<String, String>, Boolean> result =
        findIndexConfig(cls, indexName, suppliedConfig, config.getParams());
    boolean createdNow = false;
    if (result.other()) { // Ok, we need to create this config
      synchronized (this) { // Were we the first ones to get here?
        Map<String, String> existing = indexStore.get(cls, indexName);
        if (existing != null) {
          // No, someone else made it before us, cool
          assertConfigMatches(
              getIndexProvider(existing.get(PROVIDER)), indexName, existing, result.first());
          return Pair.of(result.first(), false);
        }

        // We were the first one here, let's create this config
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        try {
          executorService.submit(new IndexCreatorJob(cls, indexName, result.first())).get();
          indexStore.set(cls, indexName, result.first());
          createdNow = true;
        } catch (ExecutionException ex) {
          throw new TransactionFailureException(
              "Index creation failed for " + indexName + ", " + result.first(), ex.getCause());
        } catch (InterruptedException ex) {
          Thread.interrupted();
        } finally {
          executorService.shutdownNow();
        }
      }
    }
    return Pair.of(result.first(), createdNow);
  }
Beispiel #25
0
  /** Closes resources. */
  @Override
  public void close() {

    if (closed.compareAndSet(false, true) && numThreads > 0) {

      LOG.log(Level.FINEST, "Closing ThreadPoolStage {0}: begin", this.name);

      executor.shutdown();

      boolean isTerminated = false;
      try {
        isTerminated = executor.awaitTermination(SHUTDOWN_TIMEOUT, TimeUnit.MILLISECONDS);
      } catch (final InterruptedException ex) {
        LOG.log(Level.WARNING, "Interrupted closing ThreadPoolStage " + this.name, ex);
      }

      if (!isTerminated) {
        final List<Runnable> droppedRunnables = executor.shutdownNow();
        LOG.log(
            Level.SEVERE,
            "Closing ThreadPoolStage {0}: Executor did not terminate in {1} ms. Dropping {2} tasks",
            new Object[] {this.name, SHUTDOWN_TIMEOUT, droppedRunnables.size()});
      }

      LOG.log(Level.FINEST, "Closing ThreadPoolStage {0}: end", this.name);
    }
  }
Beispiel #26
0
  public static void main(String[] args) {

    ExecutorService threadPool = Executors.newSingleThreadExecutor();
    Future<Integer> future =
        threadPool.submit(
            new Callable<Integer>() {
              public Integer call() throws Exception {
                while (!Thread.interrupted()) {
                  Thread.sleep(1000); // 必须要有,不然cancel方法无效
                  System.out.println("call");
                }

                return 0;
              }
            });

    try {
      Thread.sleep(3000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    // threadPool.shutdown(); // 不会停止
    threadPool.shutdownNow();
    // future.cancel(true); // 停止

  }
    private HttpResponse executeRequest(
        final Callable<HttpResponse> task, final long timeout, final TimeUnit unit)
        throws TimeoutException, IOException {
      ExecutorService executor = Executors.newSingleThreadExecutor();
      try {
        Throwable lastCause = null;
        long endTime = System.currentTimeMillis() + unit.toMillis(timeout);
        while (System.currentTimeMillis() < endTime) {
          Future<HttpResponse> result = executor.submit(task);
          try {
            return result.get(timeout, unit);
          } catch (InterruptedException ex) {
            throw new IllegalStateException(ex);
          } catch (ExecutionException ex) {
            lastCause = ex.getCause();

            // HttpURLConnection throws FileNotFoundException on 404 so handle this
            if (lastCause instanceof FileNotFoundException) {
              HttpResponse httpResult = new HttpResponse();
              httpResult.setStatusCode(HttpURLConnection.HTTP_NOT_FOUND);
              return httpResult;
            } else {
              continue;
            }
          }
        }
        TimeoutException toex = new TimeoutException();
        if (lastCause != null) {
          toex.initCause(lastCause);
        }
        throw toex;
      } finally {
        executor.shutdownNow();
      }
    }
 public void stop() throws IOException {
   listenerServer.stop();
   messageReceiver.close(false);
   if (!executorService.isShutdown()) {
     executorService.shutdownNow();
   }
 }
Beispiel #29
0
 public static void main(String[] args) {
   final BlockingQueue<Character> bq;
   bq = new ArrayBlockingQueue<Character>(26);
   final ExecutorService executor = Executors.newFixedThreadPool(2);
   Runnable producer =
       () -> {
         for (char ch = 'A'; ch <= 'Z'; ch++) {
           try {
             bq.put(ch);
             System.out.printf("%c produced by " + "producer.%n", ch);
           } catch (InterruptedException ie) {
           }
         }
       };
   executor.execute(producer);
   Runnable consumer =
       () -> {
         char ch = '\0';
         do {
           try {
             ch = bq.take();
             System.out.printf("%c consumed by " + "consumer.%n", ch);
           } catch (InterruptedException ie) {
           }
         } while (ch != 'Z');
         executor.shutdownNow();
       };
   executor.execute(consumer);
 }
  @Test
  public void testFlush() throws Exception {
    final int registerCount = 1000;
    ExecutorService pool = Executors.newCachedThreadPool();
    try {
      final HeapSizeManager underTest = new HeapSizeManager(100l, 100);
      final AtomicBoolean allOperationsDone = new AtomicBoolean();
      final LinkedBlockingQueue<Long> registeredEvents = new LinkedBlockingQueue<>();
      Future<?> writeFuture =
          pool.submit(
              new Runnable() {
                @Override
                public void run() {
                  try {
                    for (int i = 0; i < registerCount; i++) {
                      registeredEvents.offer(underTest.registerOperationWithHeapSize(1));
                    }
                    underTest.flush();
                    allOperationsDone.set(true);
                    synchronized (allOperationsDone) {
                      allOperationsDone.notify();
                    }
                  } catch (InterruptedException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                  }
                }
              });
      Thread.sleep(100);
      Future<?> readFuture =
          pool.submit(
              new Runnable() {
                @Override
                public void run() {
                  try {
                    for (int i = 0; i < registerCount; i++) {
                      Long registeredId = registeredEvents.poll(1, TimeUnit.SECONDS);
                      if (registeredId == null) {
                        i--;
                      } else {
                        if (i % 50 == 0) {
                          // Exercise the .offer and the flush() in the writeFuture.
                          Thread.sleep(40);
                        }
                        underTest.markCanBeCompleted(registeredId);
                      }
                    }
                  } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                  }
                }
              });

      readFuture.get(30, TimeUnit.SECONDS);
      writeFuture.get(30, TimeUnit.SECONDS);
      assertTrue(allOperationsDone.get());
    } finally {
      pool.shutdownNow();
    }
  }