GUC-6 Callable 接口
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask; /*
* 一、创建执行线程的方式三:实现 Callable 接口。 相较于实现 Runnable 接口的方式,方法可以有返回值,并且可以抛出异常。
*
* 二、执行 Callable 方式,需要 FutureTask 实现类的支持,用于接收运算结果。 FutureTask 是 Future 接口的实现类
*/
public class TestCallable { public static void main(String[] args) {
ThreadDemo td = new ThreadDemo(); //1.执行 Callable 方式,需要 FutureTask 实现类的支持,用于接收运算结果。
FutureTask<Integer> result = new FutureTask<>(td); new Thread(result).start(); //2.接收线程运算后的结果
try {
Integer sum = result.get(); //FutureTask 可用于 闭锁
System.out.println(sum);
System.out.println("------------------------------------");
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
} } class ThreadDemo implements Callable<Integer>{ @Override
public Integer call() throws Exception {
int sum = 0; for (int i = 0; i <= 100000; i++) {
sum += i;
} return sum;
} } /*class ThreadDemo implements Runnable{ @Override
public void run() {
} }*/
GUC-6 Callable 接口的更多相关文章
- Callable接口、Runable接口、Future接口
1. Callable与Runable区别 Java从发布的第一个版本开始就可以很方便地编写多线程的应用程序,并在设计中引入异步处理.Thread类.Runnable接口和Java内存管理模型使得多线 ...
- 线程池的应用及Callable接口的使用
public interface Executor { /** * Executes the given command at some time in the future. The comman ...
- [改善Java代码]异步运算考虑使用Callable接口
多线程有两种实现方式: 一种是实现Runnable接口,另一种是继承Thread类,这两种方式都有缺点,run方法没有返回值,不能抛出异常(这两个缺点归根到底是Runable接口的缺陷,Thread也 ...
- 多线程——实现Callable接口
前两篇博客(多线程--继承Thread类.多线程--实现Runnable接口 )介绍了java使用线程的两种方法.这篇博客继续介绍第三种方法--实现Callable接口. 先说一下Runnable和C ...
- Java多线程之Callable接口的实现
Callable 和 Future接口 Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其它线程执行的任务. Callable和Runn ...
- 使用Callable接口创建线程和使用线程池的方式创建线程
1.使用Callable接口的方式实现多线程,这是JDK5.0新增的一种创建多线程的方法 package com.baozi.java2; import java.util.concurrent.Ca ...
- 创建多线程的第二种方法实现Callable接口
1.实现Callable接口,重写call方法,有返回值 2.通过实现Callable接口创建的线程只能在线程池中使用. 3.返回值就是run方法返回的对象 4.通过future.get()可以获取到 ...
- Callable接口--有返回值的线程
Callable java5之前是没有返回值的,Java5新增了Callable接口获得线程的返回值,可返回值的任务必须实现Callable接口,类似的,无返回值的任务必须Runnable接口.Cal ...
- Future接口和Callable接口以及FeatureTask详解
类继承关系 Callable接口 @FunctionalInterface public interface Callable<V> { V call() throws Exception ...
- 5、JUC--实现 Callable 接口
Callable接口 Java 5.0 在 java.util.concurrent 提供了一个新的创建执行 线程的方式:Callable 接口 Callable 接口类似于 Runnable ...
随机推荐
- Nginx+Tomcat+Memcache实现负载均衡及Session共享
第一部分 环境介绍 部署环境: Host1:Nginx.Memcached.Tomcat1 Host2:Tomcat2 Tomcat_version:8.0.38 第二部分 Nginx+Tomcat实 ...
- [大数据]-hadoop2.8和spark2.1完全分布式搭建
一.前期准备工作: 1.安装包的准备: VMware(10.0版本以上) : 官方网站:https://www.vmware.com/cn.html 官方下载地址:http://www.vmware. ...
- Hadoop基础-MapReduce的工作原理第一弹
Hadoop基础-MapReduce的工作原理第一弹 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在本篇博客中,我们将深入学习Hadoop中的MapReduce工作机制,这些知识 ...
- UIScrollView的contentSize与contentOffset
UIScrollView为了显示多于一个屏幕的内容或者超过你能放在内存中的内容. Scroll View为你处理缩小放大手势,UIScrollView实现了这些手势,并且替你处理对于它们的探测和回应. ...
- 安装VisualSVN Server 报错The specified TCP port is occupied
安装过程中报错,如下图所示. The specified TCP port is occupied by another service.Please stop that service or use ...
- HTTP 错误 500.19 请求的页面的相关配置数据无效 解决办法
"HTTP 错误 500.19 请求的页面的相关配置数据无效" 解决办法 HTTP 错误 500.19 - Internal Server Error无法访问请求的页面,因为该 ...
- classpath 及读取 properties 文件
java代码中获取项目的静态文件,如获取 properties 文件内容是必不可少的. Spring 下只需要通过 @Value 获取配置文件值 <!-- 资源文件--> <util ...
- 6.redis的分布式锁
https://www.cnblogs.com/linjiqin/p/8003838.html
- 2018年9月22日CCPC吉林站参赛总结
发现思维题是硬伤,代码能力是硬伤,对知识点的理解不深刻是硬伤 接下来要做的就是 1.熟悉每一个知识点,把每一个知识点和实现它的代码联系在一起学习 2.多见题,看看他们是怎么考察这些知识点的,等比赛的时 ...
- Spring Boot1.5X升级到2.0
配置文件 大量的Servlet专属的server.* properties被移到了server.servlet下 拦截器 public class MyWebMvcConfigurerAdapter ...