Java8-Executors-No.03
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的更多相关文章
- Java8新特性【转】
		地址:http://ifeve.com/java-8-features-tutorial/ 1.简介 毫无疑问,Java 8是自Java 5(2004年)发布以来Java语言最大的一次版本升级,Ja ... 
- 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 ... 
- Java 8 特性 – 终极手册
		简介 毫无疑问,Java 8的发布是自Java 5(它的发布已远在2004年)以来在Java世界中最重大的事情.它带来了超多的新特性,这些特性分别被加入到Java语言本身.Java编译器.类库.工具类 ... 
- Spark通过YARN提交任务不成功(包含YARN cluster和YARN client)
		无论用YARN cluster和YARN client来跑,均会出现如下问题. [spark@master spark-1.6.1-bin-hadoop2.6]$ jps 2049 NameNode ... 
- Java8并发教程:Threads和Executors
		来之:ImportNew 欢迎阅读我的Java8并发教程的第一部分.这份指南将会以简单易懂的代码示例来教给你如何在Java8中进行并发编程.这是一系列教程中的第一部分.在接下来的15分钟,你将会学会如 ... 
- Java8系列  (七)  CompletableFuture异步编程
		概述 Java8之前用 Future 处理异步请求, 当你需要获取任务结果时, 通常的做法是调用 get(long timeout, TimeUnit unit) 此方法会阻塞当前的线程, 如果任务 ... 
- Java8 通关攻略
		点赞+收藏 就学会系列,文章收录在 GitHub JavaEgg ,N线互联网开发必备技能兵器谱 Java8早在2014年3月就发布了,还不得全面了解下 本文是用我拙劣的英文和不要脸的这抄抄那抄抄,熬 ... 
- 深度解析Java8 – AbstractQueuedSynchronizer的实现分析(上)
		本文首发在infoQ :www.infoq.com/cn/articles/jdk1.8-abstractqueuedsynchronizer 前言: Java中的FutureTask作为可异步执行任 ... 
- Function接口 – Java8中java.util.function包下的函数式接口
		Introduction to Functional Interfaces – A concept recreated in Java 8 Any java developer around the ... 
- Java8 十大新特性详解(转)
		本教程将Java8的新特新逐一列出,并将使用简单的代码示例来指导你如何使用默认接口方法,lambda表达式,方法引用以及多重Annotation,之后你将会学到最新的API上的改进,比如流,函数式接口 ... 
随机推荐
- VirtualBox中CentOS7.2 网络配置(固定IP+联网)(亲测有效)
			https://www.linuxidc.com/Linux/2018-04/151924.htm 一.前言 用虚拟机装Linux系统时,经常会出现一些问题.比如:从主机到虚拟机之间网络 ... 
- jquery绑定input的change事件
			### jquery绑定input的change事件 背景:在做一个登录页时,如果用户未输入验证码则无法点击登录按钮,所以想到了用input的change事件,但是在写完后发现无法监听input值的改 ... 
- SAS学习笔记45 宏系统选项及其他
			关于宏的系统选项 MCOMPILENOTE=NONE|NOAUTOCALL|ALL 该系统选项控制是否在日志当中显示宏程序编译时的信息,默认值为NONE,也就是不显示.其中NOAUTOCALL针对的是 ... 
- Django——关于项目开发遇到的一些小技巧
			目录 头像图片 js获取网站信息 js获取前端信息 Dj获取刚写入的数据的信息 js跳转新链接 头像图片对象的显示 在做到根据登陆ID决定用户头像的时候,加载静态文件的{% static ‘xxxxx ... 
- 导入别的项目到我的eclipse上出现红色感叹号问题
			项目红色感叹号问题问题 一般我们在导入别的项目到我的eclipse上面会发现,项目上面有红色的错误 原因 因为我电脑上的 jdk版本和别人电脑jdk版本不一样,那么对于的jre版本也不 ... 
- Vs code 下设置python tasks.json
			{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.jso ... 
- Intercity Travelling CodeForces - 1009E (组合计数)
			大意: 有一段$n$千米的路, 每一次走$1$千米, 每走完一次可以休息一次, 每连续走$x$次, 消耗$a[1]+...+a[x]$的能量. 休息随机, 求消耗能量的期望$\times 2^{n-1 ... 
- 正则表达式的运行原理详解(NFA,多分支原理)
			原文:https://blog.csdn.net/yx0628/article/details/82722166 
- (二十)SpringBoot之集成mybatis:使用mybatis注解
			一.使用mybatis注解的集成 1.1 引入maven依赖 <dependencies> <dependency> <groupId>org.springfram ... 
- TCP/IP及http协议 SOAP REST
			TCP/IP及http协议: TCP/IP协议主要解决数据如何在网络中传输, 而HTTP是应用层协议,主要解决如何包装数据 SOAP:简单对象访问协议(Simple Object Access Pro ... 
