第八部分 返回结果的线程

返回结果的线程

等待单个线程

java.util.concurrent.Callable接口与java.util.concurrent.Future类相互配合,可以实现Future模式。Future模式在请求发送时,会产生一个Future对象给请求方。Future对象的作用类似于代理,所代理的真实任务在一个线程继续进行。真实任务完成之后,把结果放到Future对象之中。

Callable是一个接口,与Runnable类似。包含一个必须实现的方法call(),可以理解为线程体。不过Callable工作完成后,可以传回结果对象。在我们前面开发的多线程程序中,线程一旦被开启,就如同“脱缰野马”一般不能被父线程调用,能够返回结果给父线程的子线程。对于父线程来说。虽然子线程仍然在异步执行,但是无疑部分地达到了。同步调用的效果。

Future类的重要方法包括:

get()获取数据对象,如果数据没有加载,就会阻塞直到取到数据

cancel().取消数据加载。

下面代码演示Callable和Future类配合实现返回结果的线程。【TestFuture

/** * TestFuture.java * 版权所有(C) 2011 cuiran2001@163.com * 创建:崔冉 2011-1-14 下午03:34:55 */ package com.cayden.thread834; 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; /** * @author 崔冉 * @version 1.0.0 * @desc */ public class TestFuture { /** * @param args * @throws ExecutionException * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException, ExecutionException { // TODO Auto-generated method stub final ExecutorService exec=Executors.newFixedThreadPool(3); Callable<String> call=new Callable<String>() { public String call() throws Exception{ Thread.sleep(1000*6); return "Thread is finished"; } }; Future<String> task=exec.submit(call); System.out.println("开始等待子线程结束"); String obj=task.get(); System.out.println("子线程返回值:"+obj); exec.shutdown(); } }

运行结果:

开始等待子线程结束 子线程返回值:Thread is finished

等待一组线程

如果需要等待一组线程返回运行结果,则需要使用java.util.concurrent.CompletionService接口.java.util.concurrent.ExecutorCompletionService类实现了CompletionService接口。ExecutorCompletionService类的构造方法要求传入线程池对象(即Executor或者ExecutorService接口的实例),从而具备了从线程池中取出已经运行完毕的任务结果(即Future类的实例)的能力。

CompletionService接口定义了以下方法。

方法摘要

Future<V>poll()

获取并移除表示下一个已完成任务的Future,如果不存在这样的任务,则返回null。

Future<V>poll(longtimeout,TimeUnitunit)

获取并移除表示下一个已完成任务的Future,如果目前不存在这样的任务,则将等待指定的时间(如果有必要)。

Future<V>submit(Callable<V>task)

提交要执行的值返回任务,并返回表示挂起的任务结果的Future。

Future<V>submit(Runnabletask,Vresult)

提交要执行的Runnable任务,并返回一个表示任务完成的Future,可以提取或轮询此任务。

Future<V>take()

获取并移除表示下一个已完成任务的Future,如果目前不存在这样的任务,则等待。

下面代码演示向一个容量为10的线程池中提交5个任务,主线程等待5个线程全部运行完毕,并分别打印运行结果。【TestCompletionService

/** * TestCompletionService.java * 版权所有(C) 2011 cuiran2001@163.com * 创建:崔冉 2011-1-14 下午04:03:31 */ package com.cayden.thread834; import java.util.Date; import java.util.concurrent.Callable; import java.util.concurrent.CompletionService; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorCompletionService; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; /** * @author 崔冉 * @version 1.0.0 * @desc */ public class TestCompletionService { /** * @param args * @throws InterruptedException * @throws ExecutionException */ public static void main(String[] args) throws InterruptedException, ExecutionException { // TODO Auto-generated method stub ExecutorService exec=Executors.newFixedThreadPool(10); CompletionService<String> serv=new ExecutorCompletionService<String>(exec); for(int i=0;i<5;i++){ Callable<String> runner=new Callable<String>() { public String call() throws Exception{ Thread.sleep((long)(Math.random()*10000)); return "Thread "+Thread.currentThread().getName()+" is finished"; } }; serv.submit(runner); } Thread.sleep(1000*2); System.out.println("开始等待子线程结束"); for(int i=0;i<5;i++){ Future<String> task=serv.take(); String obj=task.get(); System.out.println(new Date()+" "+obj); } System.out.println("子线程全部结束"); exec.shutdown(); } }

运行结果:

开始等待子线程结束 Thu Jan 20 14:43:27 CST 2011 Thread pool-1-thread-2 is finished Thu Jan 20 14:43:27 CST 2011 Thread pool-1-thread-3 is finished Thu Jan 20 14:43:27 CST 2011 Thread pool-1-thread-5 is finished Thu Jan 20 14:43:28 CST 2011 Thread pool-1-thread-1 is finished Thu Jan 20 14:43:29 CST 2011 Thread pool-1-thread-4 is finished 子线程全部结束

<!--EndFragment-->
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值