import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; public class Executors3 { public static void main(String[] args) throws InterruptedException, ExecutionException {
test1();
// test2();
// test3(); // test4();
// test5();
} private static void test5() throws InterruptedException, ExecutionException {
ExecutorService executor = Executors.newWorkStealingPool(); List<Callable<String>> callables = Arrays.asList(
callable("task1", 2),
callable("task2", 1),
callable("task3", 3)); String result = executor.invokeAny(callables);
System.out.println(result); executor.shutdown();
} private static Callable<String> callable(String result, long sleepSeconds) {
return () -> {
TimeUnit.SECONDS.sleep(sleepSeconds);
return result;
};
} private static void test4() throws InterruptedException {
ExecutorService executor = Executors.newWorkStealingPool(); List<Callable<String>> callables = Arrays.asList(
() -> "task1",
() -> "task2",
() -> "task3"); executor.invokeAll(callables)
.stream()
.map(future -> {
try {
return future.get();
}
catch (Exception e) {
throw new IllegalStateException(e);
}
})
.forEach(System.out::println); executor.shutdown();
} private static void test3() {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); Runnable task = () -> {
try {
TimeUnit.SECONDS.sleep(2);
System.out.println("Scheduling: " + System.nanoTime());
}
catch (InterruptedException e) {
System.err.println("task interrupted");
}
}; executor.scheduleWithFixedDelay(task, 0, 1, TimeUnit.SECONDS);
} private static void test2() {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
Runnable task = () -> System.out.println("Scheduling: " + System.nanoTime());
int initialDelay = 0;
int period = 1;
executor.scheduleAtFixedRate(task, initialDelay, period, TimeUnit.SECONDS);
} private static void test1() throws InterruptedException {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); Runnable task = () -> System.out.println("Scheduling: " + System.nanoTime());
int delay = 3;
ScheduledFuture<?> future = executor.schedule(task, delay, TimeUnit.SECONDS); TimeUnit.MILLISECONDS.sleep(1337); long remainingDelay = future.getDelay(TimeUnit.MILLISECONDS);
System.out.printf("Remaining Delay: %sms\n", remainingDelay);
} }

最新文章

  1. j2ee部分
  2. 如何禁止 iPhone Safari video标签视频自动全屏?
  3. asp.net 微信企业号办公系统-流程设计--流转条件设置(路由)
  4. Locking
  5. 近期刷题的c语言总结。
  6. iOS开发zhiATM机的设计与实现
  7. 从零开始用 Flask 搭建一个网站(一)
  8. 算法训练 K好数 数位DP+同余定理
  9. HDU 1729
  10. INV_TXN_MANAGER_PUB.PROCESS_TRANSACTIONS
  11. mysql8.0CTE实现递归查询
  12. 2016年蓝桥杯省赛A组c++第2题(暴力求解)
  13. 2019寒假算法基础集训营1 - B 小a与&quot;204&quot;
  14. 如何在taro的map循环中使用if条件渲染
  15. 主频3.0 1g内存是什么意思
  16. Android-BitmapUtil工具类
  17. 无法正常下载Nuget 包的问题
  18. android 模仿今日头条ViewPager+TabLayout
  19. jquery on方法(事件委托)
  20. LVS,HAPROXY,NGINX各自的优缺点

热门文章

  1. K8S从入门到放弃系列-(14)Kubernetes集群Dashboard部署
  2. Java基础---JavaJShell脚本工具
  3. HTTP最常见的请求头
  4. go hello world第一个程序
  5. Python开发【第三章】:函数介绍
  6. Python开发【第三章】:编码转换
  7. yii2 migrate 数据库迁移的简单分享
  8. 【BFS】斗地主
  9. Unity性能优化-对象池
  10. (十六)SpringBoot之使用 Caching- - EhCache