将线程放到线程池中 要使用线程从里面拿即可
package com.mashibing.juc.c_000_threadbasic;
import java.util.concurrent.*;
public class T02_HowToCreateThread {
static class MyCall implements Callable<String> {
@Override
public String call() {
System.out.println("Hello MyCall");
return "success";
}
}
//启动线程的5种方式
public static void main(String[] args) throws Exception {
ExecutorService service = Executors.newCachedThreadPool();
// lambda表达式表示要执行的任务(方法)
// 无返回值的执行(Runnable接口)
service.execute(() -> {
System.out.println("Hello ThreadPool");
});
// 带返回值的执行(Callable接口)
Future<String> f = service.submit(new MyCall());
String s = f.get();
System.out.println(s);
service.shutdown();
}
}