Callable
- Runnable没有返回值:public abstract void run();,Callable可以有返回值:V call() throws Exception;
- 可以抛出异常
- 方法不同,run()/call()
- 源码
| public interface Callable<V> { |
| |
| |
| |
| |
| |
| |
| V call() throws Exception; |
| } |
Future
- Future只是一个接口
- 能判断任务是否完成;能够中断任务;能够获取任务执行结果。
- 源码
| public interface Future<V> { |
| |
| boolean cancel(boolean mayInterruptIfRunning); |
| |
| boolean isCancelled(); |
| |
| boolean isDone(); |
| |
| V get() throws InterruptedException, ExecutionException; |
| |
| V get(long timeout, TimeUnit unit) |
| throws InterruptedException, ExecutionException, TimeoutException; |
| } |
FutureTask
| public interface RunnableFuture<V> extends Runnable, Future<V> { |
| void run(); |
| } |
| public class FutureTask<V> implements RunnableFuture<V> |
RunnableFuture继承了Runnable接口和Future接口,而FutureTask实现了RunnableFuture接口。所以它既可以作为Runnable被线程执行,又可以作为Future得到Callable的返回值。
| public FutureTask(Callable<V> callable) { |
| if (callable == null) |
| throw new NullPointerException(); |
| this.callable = callable; |
| this.state = NEW; |
| } |
| |
| public FutureTask(Runnable runnable, V result) { |
| this.callable = Executors.callable(runnable, result); |
| this.state = NEW; |
| } |
| import java.util.concurrent.*; |
| |
| class MyCallable { |
| public static void main(String[] args) throws InterruptedException { |
| ExecutorService executor = Executors.newCachedThreadPool(); |
| Task task = new Task(); |
| Future<Integer> result = executor.submit(task); |
| executor.shutdown(); |
| |
| TimeUnit.SECONDS.sleep(1); |
| |
| System.out.println("主线程在执行任务"); |
| |
| try { |
| System.out.println("task运行结果" + result.get()); |
| } catch (InterruptedException | ExecutionException e) { |
| e.printStackTrace(); |
| } |
| |
| System.out.println("所有任务执行完毕"); |
| } |
| } |
| |
| class Task implements Callable<Integer> { |
| @Override |
| public Integer call() throws Exception { |
| System.out.println("子线程在进行计算"); |
| TimeUnit.SECONDS.sleep(2); |
| int sum = 0; |
| for (int i = 0; i < 100; i++) |
| sum += i; |
| return sum; |
| } |
| } |
| import java.util.concurrent.Callable; |
| import java.util.concurrent.ExecutionException; |
| import java.util.concurrent.FutureTask; |
| |
| class MyCallable { |
| public static void main(String[] args) throws ExecutionException, InterruptedException { |
| |
| |
| |
| |
| MyThread myThread = new MyThread(); |
| FutureTask<Integer> integerFutureTask = new FutureTask<>(myThread); |
| |
| new Thread(integerFutureTask, "A").start(); |
| new Thread(integerFutureTask, "B").start(); |
| |
| Integer o = integerFutureTask.get(); |
| System.out.println(o); |
| } |
| } |
| |
| class MyThread implements Callable<Integer> { |
| @Override |
| public Integer call() throws Exception { |
| System.out.println("call()"); |
| return 7; |
| } |
| } |
本文作者:n1ce2cv
本文链接:https://www.cnblogs.com/sprinining/p/15485235.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步