Java学习笔记之Callable和Future

Callable

一个接口,类似于Runnable,两者都是为那些其实力可能被另一个线程执行的类设计的。但是Runnable不能反回结果,并且不能抛出经过检查的异常。而Callable可以返回结果并且能抛出异常。

Future

一个接口,表示返回的结果。FutureTask是Future的一个实现。
示例:

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

public class CallableAndFutureTest {

    public static void main(String[] args){
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return new Random().nextInt(100);
            }
        };

        FutureTask<Integer> future = new FutureTask<Integer>(callable);
        new Thread(future).start();
        try{
            Thread.sleep(5000);
            System.out.println(future.get());
        }catch(InterruptedException e){
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

FutureTask实现了两个接口,Runnable和Future,所以它既可以作为Runnable被线程执行,又可以作为Future得到Callable的返回值。
通过ExecutorService的submit方法执行Callable:

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

public class CallableAndFutureTest {

    public static void main(String[] args){
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return new Random().nextInt(100);
            }
        };

        ExecutorService threadPool = Executors.newSingleThreadExecutor();
        Future<Integer> future = threadPool.submit(callable);

        try{
            Thread.sleep(5000);
            System.out.println(future.get());
        }catch(InterruptedException e){
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

执行多个带返回值的任务,并取得返回值

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

public class CallableAndFutureTest {

    public static void main(String[] args){

        ExecutorService threadPool = Executors.newCachedThreadPool();
        List<Future<Integer>> list = new ArrayList<Future<Integer>>();
        for(int i=0;i<5;i++){
            Future<Integer> future = threadPool.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    return new Random().nextInt(1000);
                }
            });
            list.add(future);
        }

        try{
            Thread.sleep(5000);
            for(Future<Integer> future : list){
                System.out.println(future.get());
            }
        }catch(InterruptedException e){
            e.printStackTrace();
        }catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

本文参考自http://blog.csdn.net/ghsau/article/details/7451464

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值