package com.roocon.thread.t2;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask; public class Demo4 implements Callable<Integer>{ public static void main(String[] args) throws ExecutionException, InterruptedException {
Demo4 d = new Demo4();
FutureTask<Integer> futureTask = new FutureTask<Integer>(d); //将线程任务封装成futureTask对象
Thread thread = new Thread(futureTask); //与runnable类似,将封装好的futureTask对象作为参数传入Thread类,这样最终会调用futureTask中定义的任务。
thread.start(); Integer result = futureTask.get(); System.out.println("执行结果为"+result); } @Override public Integer call() throws Exception { System.out.println("正在进行紧张的计算"); Thread.sleep(1000); return 4; } }

运行结果:

正在进行紧张的计算
执行结果为4

源码解读:

FutureTask实现了RunnableFuture接口,RunnableFuture接口又继承了Runnable接口。这样,最终会调用它定义好的run方法。
public class FutureTask<V> implements RunnableFuture<V> {
...
} public interface RunnableFuture<V> extends Runnable, Future<V> {
/**
* Sets this Future to the result of its computation
* unless it has been cancelled.
*/
void run();
}

实现Callable接口实现多线程的更多相关文章

  1. 通过Callable接口实现多线程

    一.通过Callable接口实现多线程 c.实现Callable重写call方法 实现Callable和实现Runnable类似,但是功能更强大,具体表现在 a.可以在任务结束后提供一个返回值,Run ...

  2. 使用Runnable和Callable接口实现多线程的区别

    使用Runnable和Callable接口实现多线程的区别 先看两种实现方式的步骤: 1.实现Runnable接口 public class ThreadDemo{ public static voi ...

  3. callable接口的多线程实现方式

    package com.cxy.juc; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionExce ...

  4. 使用Callable接口创建线程和使用线程池的方式创建线程

    1.使用Callable接口的方式实现多线程,这是JDK5.0新增的一种创建多线程的方法 package com.baozi.java2; import java.util.concurrent.Ca ...

  5. 使用Thread类和Runnable接口实现多线程的区别

    使用Thread类和Runnable接口实现多线程的区别 先看两种实现方式的步骤: public class ThreadDemo{ public static void main(String[] ...

  6. 多线程——实现Callable接口

    前两篇博客(多线程--继承Thread类.多线程--实现Runnable接口 )介绍了java使用线程的两种方法.这篇博客继续介绍第三种方法--实现Callable接口. 先说一下Runnable和C ...

  7. 实现多线程的方式之实现Callable接口

    package com.hls.juc; import java.util.concurrent.Callable;import java.util.concurrent.ExecutionExcep ...

  8. 51、多线程创建的三种方式之实现Callable接口

    实现Callable接口创建线程 Callable接口是在jdk5版本中加入的,这个接口在java.util.concurrent包下面,与其他两种方式不同的地方在于使用Callable接口创建的线程 ...

  9. 多线程----Thread类,Runnable接口,线程池,Callable接口,线程安全

    1概念 1.1进程 进程指正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于运行过程中的程序,并且具有一定独立功能. 任务管理器中: 1.2线程 线程是进程中的一个执行单元 ...

随机推荐

  1. 使用 pykafka 进行消费

    kafka连接脚本 环境:python3,用到的模块有 pykafka,kazoo # coding=utf-8 import pykafka class KafkaReaderThread(obje ...

  2. 关于Vue中,使用watch同时监听两个值的实现方法

    1. 先在computed中,用需要监听的两个值(start.end)定义一个对象(dateRange) computed: { dateRange () { const { start, end } ...

  3. 对于vue中axios拦截器简单封装

    axios.interceptors.response.use( response => { // 如果返回的状态码为200,说明接口请求成功,可以正常拿到数据 // 否则的话抛出错误 if ( ...

  4. JMeter测试HBase

    在网上找了关于jmeter连接hbase的方式,主要分为两种:通过导入jar包连接(Java Request)和通过BeanShell远程连接,由于刚接触jmeter没多久,对BeanShell还不熟 ...

  5. javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certificatio

    场景:Java调用PHP接口,代码部署在服务器上后,调用报错,显示PHP服务器那边证书我这边服务器不信任(我猜的). 异常信息: 2019-08-06 14:00:09,102 [http-nio-4 ...

  6. Django中使用JWT

    JWT """ 1.组成: header.payload.signature 头.载荷.签名 2.距离: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1 ...

  7. Notes for <<Thinking In Java>>

    String   Thus, when you create a toString( ) method, if the operations are simple ones that the comp ...

  8. 关于zsh-autosuggestions插件导致粘贴内容很慢的问题

    zsh开启autosuggestions 插件的时候,在终端中粘贴大量的内容的时候,会粘贴的很慢,基本上是一个字符一个字符的粘贴的. 解决方案 在.zshrc文件中配置以下内容: # This spe ...

  9. G1垃圾收集器角色划分与重要概念详解【纯理论】

    继续接着上一次[https://www.cnblogs.com/webor2006/p/11129326.html]对G1进行理论化的学习,上一次学到了G1收集器的堆结构,回忆下: 接着继续对它进行了 ...

  10. k8s的监控

    监控 1.资源指标和资源监控 一个集群系统管理离不开监控,同样的Kubernetes也需要根据数据指标来采集相关数据,从而完成对集群系统的监控状况进行监测.这些指标总体上分为两个组成:监控集群本身和监 ...