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

推荐订阅源

P
Privacy & Cybersecurity Law Blog
V
V2EX
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
Kaspersky official blog
S
Secure Thoughts
T
Tenable Blog
Security Latest
Security Latest
The Cloudflare Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
aimingoo的专栏
aimingoo的专栏
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
IT之家
IT之家
Latest news
Latest news
The Hacker News
The Hacker News
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
N
News | PayPal Newsroom
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
S
Security Affairs
S
Securelist
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
A
About on SuperTechFans

博客园 - 大力

删除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 + "任务返回的内容";
        }
    }
}