我之见--java 多线程Callable和Future

      前面我们说的线程都是没有返回值的,可以有一个耗时的操作而且要带返回值,当然我们希望如果没有返回值的之前一直阻塞线程直到返回!java 里面操作这样的接口给我们,我们只需要实现callable接口,在call方法实现耗时的操作。执行线程时候会返回一个实现Future接口的对象,Future接口提供get方法拿到Callable执行的返回值。

 

public class FutureTaskTest {

    public static void main(String[] args) {
        Callable<String> callable = new Callable<String>() {  
            public String call() throws Exception {  
                return "Run callable method";  
            }  
        };  
        FutureTask<String> future = new FutureTask<String>(callable);  
        new Thread(future).start();  
        try {  
            Thread.sleep(5000);// 可能做一些事情  
            System.out.println(" println " + future.get());  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        } catch (ExecutionException e) {  
            e.printStackTrace();  
        }  
    }

}
  FutureTask是实现的Callable接口和Runable接口的类

  我们来看下run方法:

  

public void run() {
        if (state != NEW ||
            !UNSAFE.compareAndSwapObject(this, runnerOffset,
                                         null, Thread.currentThread()))
            return;
        try {
            Callable<V> c = callable;
            if (c != null && state == NEW) {
                V result;
                boolean ran;
                try {
                    result = c.call();
                    ran = true;
                } catch (Throwable ex) {
                    result = null;
                    ran = false;
                    setException(ex);
                }
                if (ran)
                    set(result);
            }
        } finally {
            // runner must be non-null until state is settled to
            // prevent concurrent calls to run()
            runner = null;
            // state must be re-read after nulling runner to prevent
            // leaked interrupts
            int s = state;
            if (s >= INTERRUPTING)
                handlePossibleCancellationInterrupt(s);
        }
 result = c.call();
  获取执行的返回,set 方法主要给outcome赋值,当  outcome = v;当调用future.get()方法return outcome返回结果!


  Future接口也可以用于获取线程池的结果:

 ExecutorService threadPool = Executors.newSingleThreadExecutor();  
        Future<String> future = threadPool.submit(new Callable<String>() {  
            public String call() throws Exception {  
                return " ExecutorService run " ;  
            }  
        });  
        try {  
            Thread.sleep(5000);// 可能做一些事情  
            System.out.println(future.get());  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        } catch (ExecutionException e) {  
            e.printStackTrace();  
        }  
submit向线程提交一个callable相当于线程,同时得到带返回值Future对象,Future对象通get方法等到返回值。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值