Callable与Future的应用

1、Callable&Future

    一、Future取得的结果类型和Callable返回的结果类型必须一致,这是经过泛型来实现的。java

    二、Callable要采用ExecutorSevice的submit方法提交,返回的future对象能够取消任务。   dom

    三、CompletionService用于提交一组Callable任务,其take方法返回已完成的一个Callable任务对应的Future对象。ide

        > 比如我同时种了几块地的麦子,而后就等待收割。收割时,则是那块先成熟了,则先去收割哪块麦子。spa

2、代码描述

    一、CallableAndFuture.java  单个须要返回值的任务,须要等待

/** 
* @Title: CallableAndFuture.java 
* @Package com.lh.threadtest.t9 
* @Description: TODO
* @author Liu 
* @date 2018年1月17日 下午3:28:19 
* @version V1.0 
*/
package com.lh.threadtest.t9;

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

/** 
* @ClassName: CallableAndFuture 
* @Description: Callable与Future的应用
* 
* 将一个有返回值的任务交给线程池,并等待线程池执行完毕返回结果...
* 
* @author Liu
* @date 2018年1月17日 下午3:28:19 
*  
*/
public class CallableAndFuture {

	/***
	* @Title: main 
	* @Description: TODO
	* @param @param args
	* @return void
	* @throws 
	*/
	public static void main(String[] args) {
		ExecutorService executorService = Executors.newSingleThreadExecutor();
		Future<String> future = executorService.submit(new Callable<String>() {
			/* (非 Javadoc) 
			* <p>Title: call</p> 
			* <p>Description: </p> 
			* @return
			* @throws Exception 
			* @see java.util.concurrent.Callable#call() 
			*/
			@Override
			public String call() throws Exception {
				TimeUnit.SECONDS.sleep(3);
				return "hello";
			}
		});
		
		System.out.println("waiting...");
		try {
			System.out.println(future.get());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

    二、CallableAndFuture2.java  一组须要返回值的任务,须要等待

/** 
* @Title: CallableAndFuture.java 
* @Package com.lh.threadtest.t9 
* @Description: TODO
* @author Liu 
* @date 2018年1月17日 下午3:28:19 
* @version V1.0 
*/
package com.lh.threadtest.t9;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/** 
* @ClassName: CallableAndFuture 
* @Description: Callable与Future的应用
* 
* 将多个有返回值的任务交给线程池,并等待线程池执行完毕返回结果(谁先完成返回谁)...
* 
* @author Liu
* @date 2018年1月17日 下午3:28:19 
*  
*/
public class CallableAndFuture2 {

	/***
	* @Title: main 
	* @Description: TODO
	* @param @param args
	* @return void
	* @throws 
	*/
	public static void main(String[] args) {
		ExecutorService executorService = Executors.newFixedThreadPool(10);
		CompletionService<Integer> completionService = new ExecutorCompletionService<>(executorService);
		
		for(int i = 1; i <= 10; i++){
			final int seq = i;
			completionService.submit(new Callable<Integer>() {
				
				@Override
				public Integer call() throws Exception {
					TimeUnit.SECONDS.sleep(new Random().nextInt(5));
					return seq;
				}
			});
		}
		
		System.out.println("waiting...");
		
		for(int i = 1; i <= 10; i++){
			try {
				System.out.println(completionService.take().get());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}

3、注意点

    一、将一个有返回值的任务交给线程池,并等待线程池执行完毕返回结果。线程

    二、将多个有返回值的任务交给线程池,并等待线程池执行完毕返回结果(谁先完成返回谁)。code

    三、代表上看这种意义不大,由于须要等待,何不如直接调用一个方法直接等待...对象

    四、与ExecutorService.execute()方法相比,该方式存在返回值,须要线程池将结果返回。ip

相关文章
相关标签/搜索