线程池相关接口和类Callable
package src.main.java.com.qqjx.thread;
/*
/**
* 认识Callable,对Runnable进行了扩展
* 对Callable的调用,可以有返回值
* @Auther wangpeng
* @Date 2021/2/4
*/
import java.util.concurrent.*;
public class T03_Callable {
public static void main(String[] args) throws ExecutionException, InterruptedException {
Callable<String> c = new Callable() {
@Override
public String call() throws Exception {
return "Hello Callable";
}
};
ExecutorService service = Executors.newCachedThreadPool();
Future<String> future = service.submit(c); //异步 异步的意思就是主线程该干嘛干嘛 调用Callable中的call方法
System.out.println(future.get());//future.get()是阻塞的 直到有结果之后get才会返回
service.shutdown();
}
}
Hello Callable