Java的Runnable、Callable、Future、FutureTask。(1),大厂面经

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注Java)
img

正文

  1. *

  2. * @see     java.lang.Thread#run()

  3. */

  4. public abstract void run();

  5. }

Callable


Callable与Runnable的功能大致相似,Callable中有一个call()函数,但是call()函数有返回值,而Runnable的run()函数不能将结果返回给客户程序。Callable的声明如下 :

[java]  view plain  copy

  1. public interface Callable {

  2. /**

  3. * Computes a result, or throws an exception if unable to do so.

  4. *

  5. * @return computed result

  6. * @throws Exception if unable to compute a result

  7. */

  8. V call() throws Exception;

  9. }

可以看到,这是一个泛型接口,call()函数返回的类型就是客户程序传递进来的V类型。

Future


Executor就是Runnable和Callable的调度容器,Future就是对于具体的Runnable或者Callable任务的执行结果进行

**取消、**查询是否完成、获取结果、设置结果操作。get方法会阻塞,直到任务返回结果(Future简介)。Future声明如下 :

[java]  view plain  copy

  1. /**

  2. * @see FutureTask

  3. * @see Executor

  4. * @since 1.5

  5. * @author Doug Lea

  6. * @param  The result type returned by this Future’s get method

  7. */

  8. public interface Future {

  9. /**

  10. * Attempts to cancel execution of this task.  This attempt will

  11. * fail if the task has already completed, has already been cancelled,

  12. * or could not be cancelled for some other reason. If successful,

  13. * and this task has not started when cancel is called,

  14. * this task should never run.  If the task has already started,

  15. * then the mayInterruptIfRunning parameter determines

  16. * whether the thread executing this task should be interrupted in

  17. * an attempt to stop the task.     *

  18. */

  19. boolean cancel(boolean mayInterruptIfRunning);

  20. /**

  21. * Returns true if this task was cancelled before it completed

  22. * normally.

  23. */

  24. boolean isCancelled();

  25. /**

  26. * Returns true if this task completed.

  27. *

  28. */

  29. boolean isDone();

  30. /**

  31. * Waits if necessary for the computation to complete, and then

  32. * retrieves its result.

  33. *

  34. * @return the computed result

  35. */

  36. V get() throws InterruptedException, ExecutionException;

  37. /**

  38. * Waits if necessary for at most the given time for the computation

  39. * to complete, and then retrieves its result, if available.

  40. *

  41. * @param timeout the maximum time to wait

  42. * @param unit the time unit of the timeout argument

  43. * @return the computed result

  44. */

  45. V get(long timeout, TimeUnit unit)

  46. throws InterruptedException, ExecutionException, TimeoutException;

  47. }

FutureTask


FutureTask则是一个RunnableFuture,而RunnableFuture实现了Runnbale又实现了Futrue这两个接口,

[java]  view plain  copy

  1. public class FutureTask implements RunnableFuture

RunnableFuture

[java]  view plain  copy

  1. public interface RunnableFuture extends Runnable, Future {

  2. /**

  3. * Sets this Future to the result of its computation

  4. * unless it has been cancelled.

  5. */

  6. void run();

  7. }

另外它还可以包装Runnable和Callable, 由构造函数注入依赖。

[java]  view plain  copy

  1. public FutureTask(Callable callable) {

  2. if (callable == null)

  3. throw new NullPointerException();

  4. this.callable = callable;

  5. this.state = NEW;       // ensure visibility of callable

  6. }

  7. public FutureTask(Runnable runnable, V result) {

  8. this.callable = Executors.callable(runnable, result);

  9. this.state = NEW;       // ensure visibility of callable

  10. }

可以看到,Runnable注入会被Executors.callable()函数转换为Callable类型,即FutureTask最终都是执行Callable类型的任务。该适配函数的实现如下 :

[java]  view plain  copy

  1. public static  Callable callable(Runnable task, T result) {

  2. if (task == null)

  3. throw new NullPointerException();

  4. return new RunnableAdapter(task, result);

  5. }

RunnableAdapter适配器

[java]  view plain  copy

  1. /**

  2. * A callable that runs given task and returns given result

  3. */

  4. static final class RunnableAdapter implements Callable {

  5. final Runnable task;

  6. final T result;

  7. RunnableAdapter(Runnable task, T result) {

  8. this.task = task;

  9. this.result = result;

  10. }

  11. public T call() {

  12. task.run();

  13. return result;

  14. }

  15. }

由于FutureTask实现了Runnable,因此它既可以通过Thread包装来直接执行,也可以提交给ExecuteService来执行。

并且还可以直接通过get()函数获取执行结果,该函数会阻塞,直到结果返回。因此FutureTask既是Future、

**Runnable,**又是包装了Callable( 如果是Runnable最终也会被转换为Callable ), 它是这两者的合体。

简单示例


[java]  view plain  copy

  1. package com.effective.java.concurrent.task;

  2. import java.util.concurrent.Callable;

  3. import java.util.concurrent.ExecutionException;

  4. import java.util.concurrent.ExecutorService;

  5. import java.util.concurrent.Executors;

  6. import java.util.concurrent.Future;

  7. import java.util.concurrent.FutureTask;

  8. /**

  9. *

  10. * @author mrsimple

  11. *

  12. */

最后

现在正是金三银四的春招高潮,前阵子小编一直在搭建自己的网站,并整理了全套的**【一线互联网大厂Java核心面试题库+解析】:包括Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等**

image

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等**

[外链图片转存中…(img-UGDMiKEj-1713627952075)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java)
[外链图片转存中…(img-cs5BIRuP-1713627952075)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值