JUC个人学习笔记5--Callable
根据b站UP主狂神说JUC课程所写的个人学习笔记
视频地址:https://www.bilibili.com/video/BV1B7411L7tE?from=search&seid=14761503393031794075
1.可以有返回值
2.可以抛出异常
3.方法不同 run(),call()
4.代码测试
public class CallableTest { public static void main(String[] args) throws ExecutionException, InterruptedException { //new Thread(new MyThread()).start(); new Thread().start();//怎么启动callable MyThread thread = new MyThread(); //适配类 FutureTask futureTask = new FutureTask(thread); new Thread(futureTask).start(); String o = (String) futureTask.get();//获取callable的返回结果 System.out.println(o); } } class MyThread implements Callable<String> { @Override public String call() throws Exception { System.out.println("call()"); return "123"; } }
细节:
1.有缓存
2.结果可能需要等待,会阻塞