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);
} }

Java8-Executors-No.03的更多相关文章

  1. Java8新特性【转】

    地址:http://ifeve.com/java-8-features-tutorial/ 1.简介 毫无疑问,Java 8是自Java  5(2004年)发布以来Java语言最大的一次版本升级,Ja ...

  2. Java 8 Features – The ULTIMATE Guide--reference

    Now, it is time to gather all the major Java 8 features under one reference post for your reading pl ...

  3. Java 8 特性 – 终极手册

    简介 毫无疑问,Java 8的发布是自Java 5(它的发布已远在2004年)以来在Java世界中最重大的事情.它带来了超多的新特性,这些特性分别被加入到Java语言本身.Java编译器.类库.工具类 ...

  4. Spark通过YARN提交任务不成功(包含YARN cluster和YARN client)

    无论用YARN cluster和YARN client来跑,均会出现如下问题. [spark@master spark-1.6.1-bin-hadoop2.6]$ jps 2049 NameNode ...

  5. Java8并发教程:Threads和Executors

    来之:ImportNew 欢迎阅读我的Java8并发教程的第一部分.这份指南将会以简单易懂的代码示例来教给你如何在Java8中进行并发编程.这是一系列教程中的第一部分.在接下来的15分钟,你将会学会如 ...

  6. Java8系列 (七) CompletableFuture异步编程

    概述 Java8之前用 Future 处理异步请求, 当你需要获取任务结果时, 通常的做法是调用  get(long timeout, TimeUnit unit) 此方法会阻塞当前的线程, 如果任务 ...

  7. Java8 通关攻略

    点赞+收藏 就学会系列,文章收录在 GitHub JavaEgg ,N线互联网开发必备技能兵器谱 Java8早在2014年3月就发布了,还不得全面了解下 本文是用我拙劣的英文和不要脸的这抄抄那抄抄,熬 ...

  8. 深度解析Java8 – AbstractQueuedSynchronizer的实现分析(上)

    本文首发在infoQ :www.infoq.com/cn/articles/jdk1.8-abstractqueuedsynchronizer 前言: Java中的FutureTask作为可异步执行任 ...

  9. Function接口 – Java8中java.util.function包下的函数式接口

    Introduction to Functional Interfaces – A concept recreated in Java 8 Any java developer around the ...

  10. Java8 十大新特性详解(转)

    本教程将Java8的新特新逐一列出,并将使用简单的代码示例来指导你如何使用默认接口方法,lambda表达式,方法引用以及多重Annotation,之后你将会学到最新的API上的改进,比如流,函数式接口 ...

随机推荐

  1. 学习Elasticsearch原理笔记

    Elasticsearch是一个分布式可拓展的实时搜索和分析引擎 分布式实时文件存储,并将每一个字段都编入索引,使其可以被搜索 实时分析的分布式搜索引擎 可以拓展到上百台服务器,处理PB级别的结构化或 ...

  2. 【洛谷】P3980 [NOI2008]志愿者招募

    [洛谷]P3980 [NOI2008]志愿者招募 我居然现在才会用费用流解线性规划-- 当然这里解决的一类问题比较特殊 以式子作为点,变量作为边,然后要求就是变量在不同的式子里出现了两次,系数一次为+ ...

  3. insert 一条数据 然后拿出这条数据在数据库中生成的ID

    [insert 一条数据 然后拿出这条数据在数据库中生成的ID] <insert id="insert" parameterType="management&quo ...

  4. TypeScript 枚举

    我们常常会有这样的场景,比如与后端开发约定订单的状态开始是0,未结账是1,运输中是2,运输完成是3,已收货是4.这样的纯数字会使得代码缺乏可读性.枚举就用于这样的场景.枚举可以让我们定义一些名字有意义 ...

  5. EventLoop介绍

    在Netty中使用EventLoop接口代表事件循环,EventLoop是从EventExecutor和ScheduledExecutorService扩展而来,所以可以讲任务直接交给EventLoo ...

  6. wordpress 后台添加 快速编辑 栏目

    前两篇其实是同一篇,都是讲在后台添加菜单类型的http://www.ashuwp.com/courses/highgrade/664.htmlhttps://shibashake.com/wordpr ...

  7. asp.net core-11.WebHost的配置

    1.添加空的web网站 ,在目录下添加settings.json文件,在控制台上输出json的信息 public class Program { public static void Main(str ...

  8. MongoDB查询操作

    按条件查询 比较操作:$lt,$lte,$gt,$gte,$ne db.user.find({}}); $or :包含多个条件,他们之间为or的关系 ,$nor相当于or取反 db.user.find ...

  9. node 标准输入流和输出流

    使用node 在 CMD 控制台获取输入的指令: 方式一: process.stdin.resume(); process.stdin.setEncoding('utf-8'); process.st ...

  10. css滑动门技术

    滑动门的核心技术: 为了使各种特殊形状的背景能够自适应元素中文本内容的多少,以使自由拉伸滑动 利用css精灵(主要是背景位置)和盒子padding撑开宽度,以便适应不同字数的导航栏 一般经典布局 &l ...