惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
腾讯CDC
T
Threatpost
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
大猫的无限游戏
大猫的无限游戏
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
Cisco Talos Blog
Cisco Talos Blog
C
Cisco Blogs
A
Arctic Wolf
人人都是产品经理
人人都是产品经理
NISL@THU
NISL@THU
L
LINUX DO - 热门话题
爱范儿
爱范儿
GbyAI
GbyAI
The Register - Security
The Register - Security
AWS News Blog
AWS News Blog
MyScale Blog
MyScale Blog
T
Tenable Blog
Hugging Face - Blog
Hugging Face - Blog
A
About on SuperTechFans
Cyberwarzone
Cyberwarzone
量子位
Microsoft Azure Blog
Microsoft Azure Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
The Cloudflare Blog
B
Blog RSS Feed
小众软件
小众软件
D
Docker
Know Your Adversary
Know Your Adversary
Y
Y Combinator Blog
P
Privacy & Cybersecurity Law Blog
Engineering at Meta
Engineering at Meta
Latest news
Latest news
AI
AI
SecWiki News
SecWiki News
酷 壳 – CoolShell
酷 壳 – CoolShell
S
Secure Thoughts
N
News | PayPal Newsroom
The Hacker News
The Hacker News
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
博客园 - 司徒正美
L
Lohrmann on Cybersecurity
Cloudbric
Cloudbric

博客园 - 大力

删除SQL架构的用户 支付宝 公钥 应用公钥 apidoc FlexCel 插入公式和插入新行 DataTable 修改列名 删除列 调整列顺序 SQL Server CTE 递归查询全解 HTML空格占位符 如何:在 ASP.NET 网页中检测浏览器类型 VS格式化代码 Newtonsoft.Json Dell DRAC的重启方法 iTunes Web.Config中的<compilation debug="true"/> C#转VB.NET thread.join 从异步执行变成同步 Android 子线程测试 JAVA下的Thread.sleep方法一定要try 解决Android Studio 和 Android SDK Manager 无法在线更新的问题. asp.net关于页面不回发,不生成__doPostBack方法问题的完美解决方案
Java线程中带有返回值的线程Callable
大力 · 2015-05-17 · via 博客园 - 大力

在Java5之前,线程是没有返回值的,常常为了“有”返回值,破费周折,而且代码很不好写。或者干脆绕过这道坎,走别的路了。现在Java终于有可返回值的任务(也可以叫做线程)了。

可返回值的任务必须实现Callable接口,类似的,无返回值的任务必须Runnable接口。

执行Callable任务后,可以获取一个Future的对象,在该对象上调用get就可以获取到Callable任务返回的Object了。

下面是个很简单的例子:

package com.lyc

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


public class CallableFutureTest {


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CallableFutureTest test = new CallableFutureTest();
       
        // 创建一个线程池
        ExecutorService pool = Executors.newFixedThreadPool(2);
        // 创建两个有返回值的任务
        Callable c1 = test.new MyCallable("A");
        Callable c2 = test.new MyCallable("B");
       
        // 执行任务并获取Future对象
        Future f1 = pool.submit(c1);
        Future f2 = pool.submit(c2);
       
        // 从Future对象上获取任务的返回值,并输出到控制台
        System.out.println("----" + f1.get().toString());
        System.out.println("----" + f2.get().toString());
       
        // 关闭线程池
        pool.shutdown();
    }

    class MyCallable implements Callable {
        private String oid;

        MyCallable(String oid) {
            this.oid = oid;
        }

        public Object call() throws Exception {
            return oid + "任务返回的内容";
        }
    }
}