当前位置: 首页>>代码示例>>Java>>正文


Java ThreadPoolExecutor.awaitTermination方法代码示例

本文整理汇总了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;
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:25,代码来源:AsyncDiskService.java

示例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();
  }
}
 
开发者ID:dremio,项目名称:dremio-oss,代码行数:21,代码来源:AutoCloseables.java

示例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);
}
 
开发者ID:yohlulz,项目名称:MLE5109-Course-samples,代码行数:19,代码来源:Rejection.java

示例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")));
    }
 
开发者ID:FlowCI,项目名称:flow-platform,代码行数:14,代码来源:CmdManagerTest.java

示例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...");
    }
  }
}
 
开发者ID:fengchen8086,项目名称:ditb,代码行数:15,代码来源:CompactSplitThread.java

示例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");
	
}
 
开发者ID:PacktPublishing,项目名称:Java-9-Concurrency-Cookbook-Second-Edition,代码行数:52,代码来源:Main.java

示例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); 
	}
 
开发者ID:olehmberg,项目名称:winter,代码行数:77,代码来源:Parallel.java


注:本文中的java.util.concurrent.ThreadPoolExecutor.awaitTermination方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。