1.Thread类可以传入Runnable接口,而FutureTask实现了Runnable接口,并且构造参数为Callable

package com.atgu;

import java.util.concurrent.*;

class MyCallable implements Callable<Integer>{
    @Override
    public Integer call() throws Exception {
        System.out.println("进入了call()方法。。。。");
        return 1024;
    }
}
public class CallableDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> futureTask = new FutureTask<Integer>(new MyCallable());

        new Thread(futureTask,"aaa").start();
        System.out.println(futureTask.get());//获得返回值

    }
}

 

posted on 2022-03-01 14:47  upupup-999  阅读(83)  评论(0)    收藏  举报