java中Callable接口是使用示例

与Runnable区别:

        Runnable:没有返回信息,实现run()方法。

        Callable<T>: A task that returns a result and may throw an exception ,返回执行结果,抛出异常;需要实现call()方法

Future

        Future就是对于具体的Runnable或者Callable任务的执行结果进行取消、查询是否完成、获取结果、设置结果操作。get方法会阻塞,直到任务返回结果

FutureTask

         FutureTask实现了Runnbale又实现了Futrue<V>这两个接口,既是Future、Runnable,又包装了Callable。


eg:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
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;
import java.util.concurrent.FutureTask;

public class CallableTest{
 
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		
		//1 。因此FutureTask既是Future、 Runnable,又是包装了Callable( 如果是Runnable最终也会被转换为Callable ), 它是这两者的合体。
		FutureTask<Integer> futureTask = new FutureTask<>(new Callable<Integer>() {
			@Override
			public Integer call() throws Exception {
				return new Random().nextInt(100);
			}
		});
		new Thread(futureTask).start();
		System.out.println(futureTask.get());
		System.out.println("###########################################################");
		
		//2 Executor就是Runnable和Callable的调度容器,Future就是对于具体的Runnable或者Callable任务的执行结果进行取消、查询是否完成、获取结果、设置结果操作
		ExecutorService threadPool = Executors.newSingleThreadExecutor();
		Future<Integer> future = threadPool.submit(new Callable<Integer>() {
			@Override
			public Integer call() throws Exception {
				return new Random().nextInt(100);
			}
		});
		//get方法会阻塞,直到任务返回结果
		System.out.println(future.get());
		System.out.println("###########################################################");
		
		//3 Future是按照添加的顺序排列的
		ExecutorService threadPool1 = Executors.newSingleThreadExecutor();
		List<Future<Integer>> list = new ArrayList<>();
		for(int i=0;i<5;i++){
			list.add(threadPool1.submit(new Callable<Integer>() {
				@Override
				public Integer call() throws Exception {
					return new Random().nextInt(100);
				}
			}));
		}
		for(Future<Integer> fs:list){
			System.out.println(fs.get());
		}
		System.out.println("------------------------");
		
		ArrayList<Callable<Integer>> arrayList = new ArrayList<Callable<Integer>>();
		for(int i=0;i<5;i++){
			arrayList.add(new Callable<Integer>() {
				@Override
				public Integer call() throws Exception {
					return new Random().nextInt(100);
				}
			});
		}
		List<Future<Integer>> result = threadPool1.invokeAll(arrayList);
		for(Future<Integer> fs:result){
			System.out.println(fs.get());
		}
		System.out.println("###########################################################");
		
		//4  提交到CompletionService中的Future是按照完成的顺序排列的
		ExecutorService executorService = Executors.newSingleThreadExecutor();
		CompletionService<Integer> cs = new ExecutorCompletionService<>(executorService);
		for(int i=0;i<5;i++){
			final int id = i;
			cs.submit(new Callable<Integer>() {
				@Override
				public Integer call() throws Exception {
					return id;
				}
			});
		}
		for(int i=0;i<5;i++){
			System.out.println(cs.take().get());
		}
		System.out.println("###########################################################");
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值