将线程放到线程池中 要使用线程从里面拿即可

    1. package com.mashibing.juc.c_000_threadbasic;
    2. import java.util.concurrent.*;
    3. public class T02_HowToCreateThread {
    4. static class MyCall implements Callable<String> {
    5. @Override
    6. public String call() {
    7. System.out.println("Hello MyCall");
    8. return "success";
    9. }
    10. }
    11. //启动线程的5种方式
    12. public static void main(String[] args) throws Exception {
    13. ExecutorService service = Executors.newCachedThreadPool();
    14. // lambda表达式表示要执行的任务(方法)
    15. // 无返回值的执行(Runnable接口)
    16. service.execute(() -> {
    17. System.out.println("Hello ThreadPool");
    18. });
    19. // 带返回值的执行(Callable接口)
    20. Future<String> f = service.submit(new MyCall());
    21. String s = f.get();
    22. System.out.println(s);
    23. service.shutdown();
    24. }
    25. }