Java8-Executors-No.02
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class Executors2 {
public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
// test1();
// test2();
test3();
}
private static void test3() throws InterruptedException, ExecutionException, TimeoutException {
ExecutorService executor = Executors.newFixedThreadPool(1);
Future<Integer> future = executor.submit(() -> {
try {
TimeUnit.SECONDS.sleep(2);
return 123;
}
catch (InterruptedException e) {
throw new IllegalStateException("task interrupted", e);
}
});
future.get(1, TimeUnit.SECONDS);
}
private static void test2() throws InterruptedException, ExecutionException {
ExecutorService executor = Executors.newFixedThreadPool(1);
Future<Integer> future = executor.submit(() -> {
try {
TimeUnit.SECONDS.sleep(1);
return 123;
}
catch (InterruptedException e) {
throw new IllegalStateException("task interrupted", e);
}
});
executor.shutdownNow();
future.get();
}
private static void test1() throws InterruptedException, ExecutionException {
ExecutorService executor = Executors.newFixedThreadPool(1);
Future<Integer> future = executor.submit(() -> {
try {
TimeUnit.SECONDS.sleep(1);
return 123;
}
catch (InterruptedException e) {
throw new IllegalStateException("task interrupted", e);
}
});
System.out.println("future done: " + future.isDone());
Integer result = future.get();
System.out.println("future done: " + future.isDone());
System.out.print("result: " + result);
executor.shutdownNow();
}
}
Java8-Executors-No.02的更多相关文章
- 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 ...
- spark之scala程序开发(集群运行模式):单词出现次数统计
准备工作: 将运行Scala-Eclipse的机器节点(CloudDeskTop)内存调整至4G,因为需要在该节点上跑本地(local)Spark程序,本地Spark程序会启动Worker进程耗用大量 ...
- spark之java程序开发
spark之java程序开发 1.Spark中的Java开发的缘由: Spark自身是使用Scala程序开发的,Scala语言是同时具备函数式编程和指令式编程的一种混血语言,而Spark源码是基于Sc ...
- Java 8 特性 – 终极手册
简介 毫无疑问,Java 8的发布是自Java 5(它的发布已远在2004年)以来在Java世界中最重大的事情.它带来了超多的新特性,这些特性分别被加入到Java语言本身.Java编译器.类库.工具类 ...
- Java8并发教程:Threads和Executors
来之:ImportNew 欢迎阅读我的Java8并发教程的第一部分.这份指南将会以简单易懂的代码示例来教给你如何在Java8中进行并发编程.这是一系列教程中的第一部分.在接下来的15分钟,你将会学会如 ...
- [2017.02.23] Java8 函数式编程
以前学过Haskell,前几天又复习了其中的部分内容. 函数式编程与命令式编程有着不一样的地方,函数式编程中函数是第一等公民,通过使用少量的几个数据结构如list.map.set,以及在这些数据结构上 ...
- 【java8新特性】02:常见的函数式接口
Jdk8提供的函数式接口都在java.util.function包下,Jdk8的函数式类型的接口都有@FunctionInterface注解所标注,但实际上即使没有该注解标注的有且只有一个抽象方法的接 ...
- Java8新特性——新一套时间API的使用
JDK 1.0中包含了一个java.util.Date类,但是它的大多数方法已经在JDK 1.1引入Calendar类之后被弃用了.而Calendar并不比Date好多少.它们面临的问题是: 可变性: ...
- java8中CAS的增强
注:ifeve.com的同名文章为本人所发,此文在其基础做了些调整.转载请注明出处! 一.java8中CAS的增强 前些天,我偶然地将之前写的用来测试AtomicInteger和synchronize ...
随机推荐
- 生成 n 个不同的随机数且随机数区间为 [0,n)
生成 n 个不同的随机数且随机数区间为 [0,n) Java 实现 import java.util.ArrayList; import java.util.List; import java.uti ...
- [官网]Postgresql 的客户端应用 pg_config
pg_config Name pg_config -- 检索已安装版本的 PostgreSQL 的信息 Synopsis pg_config {--bindir | --includedir | -- ...
- yum源出问题,rpmdb: BDB0113 Thread/process 17276/140338032428864 failed: BDB1507 Thread died in Berkeley DB library
yum源出问题 cd /var/lib/rpm rm -f *db.* rpm --rebuilddb 重构了之后就可以用了
- day0~day13
day0 day1 day2 day4 day5 day7 day9 day10 day12 day13
- Echarts API说明文档
theme = { // 全图默认背景 // backgroundColor: 'rgba(0,0,0,0)', // 默认色板 ...
- Image splicing forgery detection combining coarse to refined convolutional neural network and adaptive clustering
粗到精的卷积神经网络与自适应聚类相结合的图像拼接篡改检测 研究方向:图像篡改检测 论文出处:ELSEVIER A类 学校:西安电子科技大学网络工程学院.重庆邮电大学计算机科学与技术学院 关键字:Spl ...
- 『Python基础』第1节 Windows环境下安装Python3.x
一. Python安装 1. 下载安装包 https://www.python.org/downloads/release/python-374/ # 3.7安装包 # 如需安装python2.7版本 ...
- spring cloud 停止服务
shutdown的默认url为host:port/shutdown,当需要停止服务时,向服务器post该请求即可,如:curl -X POST host:port/shutdown将得到形如{&quo ...
- root用户和sudo使用root权限的区别(转)
百度百科:https://baike.baidu.com/item/sudo/7337623?fr=aladdin sudo指令 功能: 以root的身分执行命令 语法: sudo 其他指令 用户: ...
- (五)Activiti之查看最新版本的流程定义
一.查看最新版本的流程定义 因为每个流程定义都可能会有好几个版本,所以有时候我们有这样的需求,查询出最新版本的流程定义的集合 第一步:我们通过Activiti接口来获取根据流程定义Version升序排 ...