本文整理汇总了Java中java.util.concurrent.ThreadPoolExecutor.awaitTermination方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadPoolExecutor.awaitTermination方法的具体用法?Java ThreadPoolExecutor.awaitTermination怎么用?Java ThreadPoolExecutor.awaitTermination使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ThreadPoolExecutor
的用法示例。
在下文中一共展示了ThreadPoolExecutor.awaitTermination方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: awaitTermination
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
* Wait for the termination of the thread pools.
*
* @param milliseconds The number of milliseconds to wait
* @return true if all thread pools are terminated without time limit
* @throws InterruptedException
*/
public synchronized boolean awaitTermination(long milliseconds)
throws InterruptedException {
long end = Time.now() + milliseconds;
for (Map.Entry<String, ThreadPoolExecutor> e:
executors.entrySet()) {
ThreadPoolExecutor executor = e.getValue();
if (!executor.awaitTermination(
Math.max(end - Time.now(), 0),
TimeUnit.MILLISECONDS)) {
LOG.warn("AsyncDiskService awaitTermination timeout.");
return false;
}
}
LOG.info("All AsyncDiskService threads are terminated.");
return true;
}
示例2: close
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
public static void close(ThreadPoolExecutor executor, Logger logger) {
executor.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
executor.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
logger.error("Pool did not terminate");
}
}
} catch (InterruptedException ie) {
logger.warn("Executor interrupted while awaiting termination");
// (Re-)Cancel if current thread also interrupted
executor.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
示例3: main
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
public static void main(String[] args) throws InterruptedException {
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 2, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(2));
executor.setRejectedExecutionHandler((task, exec) -> System.out.println(task.toString() + " : Rejected"));
// Add some tasks
for (int i = 0; i < 9; i++) {
executor.execute(new WorkerTask("Task " + (i + 1)));
// Sleep
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
executor.shutdown();
executor.awaitTermination(20, TimeUnit.SECONDS);
}
示例4: should_has_cmd_log
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
@Test
public void should_has_cmd_log() throws Throwable {
Cmd cmd = new Cmd("zone1", "agent1", CmdType.RUN_SHELL, resourcePath);
cmd.setId(UUID.randomUUID().toString());
cmdManager.execute(cmd);
ThreadPoolExecutor cmdExecutor = cmdManager.getCmdExecutor();
cmdExecutor.shutdown();
cmdExecutor.awaitTermination(60, TimeUnit.SECONDS);
// Assert.assertTrue(Files.exists(Paths.get(TEMP_LOG_DIR.toString(), cmd.getId() + ".out.zip")));
// Assert.assertTrue(Files.exists(Paths.get(TEMP_LOG_DIR.toString(), cmd.getId() + ".err.zip")));
}
示例5: waitFor
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
private void waitFor(ThreadPoolExecutor t, String name) {
boolean done = false;
while (!done) {
try {
done = t.awaitTermination(60, TimeUnit.SECONDS);
LOG.info("Waiting for " + name + " to finish...");
if (!done) {
t.shutdownNow();
}
} catch (InterruptedException ie) {
LOG.warn("Interrupted waiting for " + name + " to finish...");
}
}
}
示例6: main
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
* @param args
*/
public static void main(String[] args) throws Exception{
/*
* Create a new Executor
*/
ThreadPoolExecutor executor=(ThreadPoolExecutor)Executors.newCachedThreadPool();
/*
* Create and submit ten tasks
*/
Random random=new Random();
for (int i=0; i<10; i++) {
Task task=new Task(random.nextInt(10000));
executor.submit(task);
}
/*
* Write information about the executor
*/
for (int i=0; i<5; i++){
showLog(executor);
TimeUnit.SECONDS.sleep(1);
}
/*
* Shutdown the executor
*/
executor.shutdown();
/*
* Write information about the executor
*/
for (int i=0; i<5; i++){
showLog(executor);
TimeUnit.SECONDS.sleep(1);
}
/*
* Wait for the finalization of the executor
*/
executor.awaitTermination(1, TimeUnit.DAYS);
/*
* Write a message to indicate the end of the program
*/
System.out.printf("Main: End of the program.\n");
}
示例7: producerConsumer
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
public void producerConsumer(final Producer<T> producer, final Consumer<T> consumer)
{
boolean isOuter = startParallelProcess(this);
//final Timer tim = new Timer("Parallel.producerConsumer");
if(getNumProcessors(this)>1) {
ThreadPoolExecutor pool = new ThreadPoolExecutor(
getNumProcessors(this),
getNumProcessors(this),
0,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
public Thread newThread(Runnable r) {
return new Thread(r, "Parallel.producerConsumer thread");
}
});
//ThreadPoolExecutor pool = getExecutor(getNumProcessors());
producer.setConsumer(consumer);
producer.setPool(pool);
RunnableProgressReporter rpr = new RunnableProgressReporter();
rpr.setPool(pool);
//p.setTimer(timerToReport);
if(isOuter)
rpr.start();
// start the producer thread
ITask producerTask = new Task() {
@Override
public void execute() {
//Timer tp = new Timer("Producer", tim);
producer.execute();
//tp.stop();
}
};
run(producerTask);
// wait for the producer thread to finish
join(producerTask);
// try {
// //p.join();
//
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
//if(isOuter)
//System.out.println("Producer finished.");
// wait for the consumer threads to finish
pool.shutdown();
try {
pool.awaitTermination(1, TimeUnit.DAYS);
} catch (InterruptedException e) {
e.printStackTrace();
}
rpr.stop();
} else {
// run single-threaded
producer.setRunSingleThreaded(true);
producer.setConsumer(consumer);
producer.execute();
}
endParallelProcess(this);
}