详细解读Callable接口及Futrue接口详解

简介: 详细解读Callable接口及Futrue接口详解

目录

Callable接口Futrue接口1.使用Callable和Future的完整示例2.使用Callable和FutureTask的完整示例3.使用Runnable来获取返回结果的实现

回到顶部Callable接口

有两种创建线程的方法-一种是通过创建Thread类,另一种是通过使用Runnable创建线程。但是,Runnable缺少的一项功能是,当线程终止时(即run()完成时),我们无法使线程返回结果。为了支持此功能,Java中提供了Callable接口。

为了实现Runnable,需要实现不返回任何内容的run()方法,而对于Callable,需要实现在完成时返回结果的call()方法。请注意,不能使用Callable创建线程,只能使用Runnable创建线程。

另一个区别是call()方法可以引发异常,而run()则不能。

为实现Callable而必须重写call方法。

// Java program to illustrate Callable

// to return a random number

import java.util.Random;

import java.util.concurrent.Callable;

import java.util.concurrent.FutureTask;

class CallableExample implements Callable

{

public Object call() throws Exception

{

// Create random number generator

Random generator = new Random();

Integer randomNumber = generator.nextInt(5);

// To simulate a heavy computation,

// we delay the thread for some random time

Thread.sleep(randomNumber 1000);

return randomNumber;

}

}

回到顶部Futrue接口

当call()方法完成时,结果必须存储在主线程已知的对象中,以便主线程可以知道该线程返回的结果。为此,可以使用Future对象。将Future视为保存结果的对象–它可能暂时不保存结果,但将来会保存(一旦Callable返回)。因此,Future基本上是主线程可以跟踪进度以及其他线程的结果的一种方式。要实现此接口,必须重写5种方法,但是由于下面的示例使用了库中的具体实现,因此这里仅列出了重要的方法。

public boolean cancel(boolean mayInterrupt):用于停止任务。如果尚未启动,它将停止任务。如果已启动,则仅在mayInterrupt为true时才会中断任务。

public Object get()抛出InterruptedException,ExecutionException:用于获取任务的结果。如果任务完成,它将立即返回结果,否则将等待任务完成,然后返回结果。

public boolean isDone():如果任务完成,则返回true,否则返回false

可以看到Callable和Future做两件事-Callable与Runnable类似,因为它封装了要在另一个线程上运行的任务,而Future用于存储从另一个线程获得的结果。实际上,future也可以与Runnable一起使用。

要创建线程,需要Runnable。为了获得结果,需要future。

Java库具有具体的FutureTask类型,该类型实现Runnable和Future,并方便地将两种功能组合在一起。

可以通过为其构造函数提供Callable来创建FutureTask。然后,将FutureTask对象提供给Thread的构造函数以创建Thread对象。因此,间接地使用Callable创建线程。

1.使用Callable和Future的完整示例

package com.example.thread.callable;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import java.util.concurrent.;

/

@author: GuanBin

@date: Created in 下午11:19 2019/10/31

*/

public class TestCallable implements Callable {

private int taskNum;

public TestCallable(int taskNum) {

this.taskNum = taskNum;

}

//1,2主要区别是创建线程的方式

public static void main(String【】 args) throws ExecutionException, InterruptedException {

test1();

test2();

}

/

使用Executors.newFixedThreadPool创建线程池

@throws InterruptedException

@throws ExecutionException

/

private static void test1() throws InterruptedException, ExecutionException {

System.out.println("程序开始运行");

Date date1 = new Date();

int taskSize=5;

ExecutorService pool = Executors.newFixedThreadPool(taskSize);

List list = new ArrayList();

for (int i = 0; i < taskSize; i++) {

Callable c = new TestCallable(i);

// 执行任务并获取Future对象

Future f = pool.submit(c);

list.add(f);

}

// 关闭线程池

pool.shutdown();

// 获取所有并发任务的运行结果

for (Future f : list) {

// 从Future对象上获取任务的返回值,并输出到控制台

System.out.println("]>" + f.get().toString()); //OPTION + return 抛异常

}

Date date2 = new Date();

System.out.println("程序结束运行,程序运行时间【" + (date2.getTime() - date1.getTime()) + "毫秒】");

}

/

线程直接使用new Thread来创建

@throws ExecutionException

@throws InterruptedException

/

private static void test2() throws ExecutionException, InterruptedException {

System.out.println("程序开始运行");

Date date1 = new Date();

int taskSize=5;

FutureTask【】 randomNumberTasks = new FutureTask【5】;

List list = new ArrayList();

for (int i = 0; i < randomNumberTasks.length; i++) {

Callable c = new TestCallable(i);

// 执行任务并获取Future对象

randomNumberTasks【i】= new FutureTask(c);

Thread t = new Thread(randomNumberTasks【i】);

t.start();

}

// 获取所有并发任务的运行结果

for (Future f : randomNumberTasks) {

// 从Future对象上获取任务的返回值,并输

System.out.println("]>" + f.get().toString()); //OPTION + return 抛异常

}

Date date2 = new Date();

System.out.println("程序结束运行,程序运行时间【" + (date2.getTime() - date1.getTime()) + "毫秒】");

}

/

call方法的实现,主要用于执行线程的具体实现,并返回结果

@return

@throws Exception

/

@Override

public Object call() throws Exception {

System.out.println("]>" + taskNum + "任务启动");

Date dateTmp1 = new Date();

Thread.sleep(1000);

Date dateTmp2 = new Date();

long time = dateTmp2.getTime() - dateTmp1.getTime();

System.out.println("]>" + taskNum + "任务终止");

return taskNum + "任务返回运行结果,当前任务时间【" + time + "毫秒】";

}

}

输出

程序开始运行

]>0任务启动

程序结束运行,程序运行时间【1007毫秒】

Process finished with exit //代码效果参考:http://www.zidongmutanji.com/bxxx/177472.html

code 0

2.使用Callable和FutureTask的完整示例

// Java program to illustrate Callable and FutureTask

// for random number generation

import java.util.Random;

import java.util.concurrent.Callable;

import java.util.concurrent.FutureTask;

class CallableExample implements Callable

{

public Object call() throws Exception

{

Random generator = new Random();

Integer randomNumber = generator.nextInt(5);

Thread.sleep(randomNumber 1000);

return randomNumber;

}

}

public class CallableFutureTest

{

public static void main(String【】 args) throws Exception

{

// FutureTask is a concrete class that

// implements both Runnable and Future

FutureTask【】 randomNumberTasks = new FutureTask【5】;

for (int i = 0; i < 5; i++)

//代码效果参考:http://www.zidongmutanji.com/bxxx/563752.html

{

Callable callable = new CallableExample();

// Create the FutureTask with Callable

randomNumberTasks【i】 = new FutureTask(callable);

// As it implements Runnable, create Thread

// with FutureTask

Thread t = new Thread(randomNumberTasks【i】);

t.start();

}

for (int i = 0; i < 5; i++)

{

// As it implements Future, we can call get()

System.out.println(randomNumberTasks【i】.get());

// This method blocks till the result is obtained

// The get method can throw checked exceptions

// like when it is interrupted. This is the reason

// for adding the throws clause to main

}

}

}

启动线程后,与线程的所有交互都使用FutureTask,因为它实现了Future接口。因此,不需要存储Thread对象。使用FutureTask对象,还可以取消任务,检查任务是否完成或尝试获取结果。

3.使用Runnable来获取返回结果的实现

// Java program to illustrate Runnable

// for random number generation

import java.util.Random;

import java.util.concurrent.Callable;

import java.util.concurrent.FutureTask;

class RunnableExample implements Runnable

{

// Shared object to store result

private Object result = null;

public void run()

{

Random generator = new Random();

Integer randomNumber = generator.nextInt(5);

// As run cannot throw any Exception

try

{

Thread.sleep(randomNumber 1000);

}

catch (InterruptedException e)

{

e.printStackTrace();

}

// Store the return value in result when done

result = randomNumber;

// Wake up threads blocked on the get() method

synchronized(this)

{

notifyAll();

}

}

public synchronized Object get()

throws InterruptedException

{

while (result == null)

wait();

return result;

}

}

// Code is almost same as the previous example with a

// few changes made to use Runnable instead of Callable

public class RunnableTest

{

public static void main(String【】 args) throws Exception

{

RunnableExample【】 randomNumberTasks = new RunnableExample【5】;

for (int i = 0; i < 5; i++)

{

randomNumberTasks【i】 = new RunnableExample();

Thread t = new Thread(randomNumberTasks【i】);

t.start();

}

for (int i = 0; i < 5; i++)

System.out.println(randomNumberTasks【i】.get());

}

}

作者:guanbin —— 纵码万里千山

出处:

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

相关文章
|
缓存 前端开发 NoSQL
体育赛事直播系统中 VIP会员功能 开发实现思路及核心实现代码示例
东莞梦幻网络科技基于ThinkPHP+MySQL+Redis架构,实现体育直播平台VIP会员系统。涵盖VIP开通流程、权限控制、标识展示等功能。用户选择套餐并支付后,系统更新VIP状态并缓存权限信息,前端通过JWT+Redis验证权限,展示VIP标识与权益。数据库设计清晰,支持多种VIP等级与权限配置,代码示例完整,便于快速集成与扩展。
|
11月前
|
缓存 网络协议 Linux
RARP协议详解(Linux下反向地址解析协议入门指南)
RARP(反向地址解析协议)是一种早期网络协议,用于通过MAC地址查询IP地址,常用于无盘工作站启动时获取IP。与ARP相反,RARP实现从MAC到IP的映射。尽管现已被DHCP取代,了解RARP仍对学习Linux网络配置和网络协议演进具有重要意义。
|
存储 人工智能 弹性计算
飞天发布时刻丨阿里云基础设施助力企业全球化业务创新
丰富灵活的算力服务、稳定高可用的存储产品,覆盖了出海企业 AI 创新、 大数据分析、 应用管理和办公协同等不同场景和需求,助力客户业务全球化布局。
|
12月前
|
传感器 人工智能 算法
2025 全球 GEO 行业观察:双轮驱动(市场 + 技术),AI 时代品牌新基建的破局之道
AI重构信息分发,GEO成品牌新基建。2025年AI搜索占全球63%,传统SEO失效,生成式引擎优化(GEO)通过结构化数据、语义适配与权威构建,助力内容在AI答案中优先被引。本文解析GEO六大核心挑战与落地策略。
722 2
|
11月前
|
存储 弹性计算 应用服务中间件
2026年阿里云服务器配置选型指南:个人与企业用户适配方案
在阿里云服务器选型中,核心是根据用户类型(个人 / 企业)、业务场景(网站搭建、开发测试、生产应用)及性能需求,匹配 CPU、内存、带宽、存储的最优组合。不同用户对成本、稳定性、扩展性的诉求差异显著,个人开发者侧重性价比与易用性,企业用户则需保障性能稳定与业务连续性。本文结合实例规格特性与场景需求,提供从入门到企业级的完整选型参考。
|
数据采集 存储 算法
Python内存管理机制:垃圾回收与引用计数
Python内存管理融合引用计数与分代垃圾回收,辅以内存池优化小对象分配。通过弱引用、生成器和手动GC调控,可有效避免循环引用与内存泄漏,实现高效稳定的程序运行。
679 1
|
消息中间件 数据采集 监控
高级应用:利用DataHub构建实时数据流处理系统
【10月更文挑战第23天】在大数据时代,实时数据处理的需求日益增长。无论是金融交易、物联网设备监控,还是社交媒体分析,实时数据流处理系统都扮演着至关重要的角色。作为阿里云提供的实时数据同步服务,DataHub为开发者提供了一种高效、可靠的方式来构建实时数据流处理系统。本文将从个人的角度出发,探讨如何利用DataHub构建实时数据流处理系统,包括配置实时数据采集、与流处理引擎集成、实施数据流的实时分析和处理,以及确保系统的高可用性和扩展性。
1016 5
|
人工智能 安全 数据可视化
数字孪生 + 区块链:MyEMS 引领能源管理技术融合新趋势
MyEMS融合数字孪生与区块链技术,打造可信、透明、高效的能源管理新范式。通过实时镜像、智能预测与数据上链,实现能耗可追溯、碳排可验证、交易可信任,推动能源管理迈向智能化与价值化新时代。(238字)
662 1
|
自然语言处理 Cloud Native 安全
适应多样化需求:WASM 插件在全链路灰度发布中的应用
MSE(微服务引擎)在微服务全链路灰度场景下提供了一套成熟的功能,支持内容规则和百分比规则的灰度路由策略。
62452 133