ExecutorService

来源:互联网 发布:数据安全保护管理制度 编辑:程序博客网 时间:2024/05/29 17:31

线程执行器

public class ExecutorServiceClient {    public static void main(String[] args) throws ExecutionException, InterruptedException {        ExecutorService executorService = Executors.newFixedThreadPool(5);        executorService.execute(() -> System.out.println(Thread.currentThread().getName()));        executorService.execute(() -> System.out.println(Thread.currentThread().getName()));        Future future = executorService.submit(() -> System.out.println(Thread.currentThread().getName()));        System.out.println("future------" + future.get());        future = executorService.submit(() -> {            System.out.println(Thread.currentThread().getName());            return "我也是Callable";        });        System.out.println("future------" + future.get());        future = executorService.submit(() -> {            System.out.println(Thread.currentThread().getName());            return "我是Callable";        });        System.out.println("future------" + future.get());        executorService.submit(() -> System.out.println(Thread.currentThread().getName()));        executorService.execute(() -> System.out.println(Thread.currentThread().getName()));        executorService.execute(() -> System.out.println(Thread.currentThread().getName()));        executorService.execute(() -> System.out.println(Thread.currentThread().getName()));        executorService.execute(() -> System.out.println(Thread.currentThread().getName()));        executorService.execute(() -> System.out.println(Thread.currentThread().getName()));        executorService.execute(() -> System.out.println(Thread.currentThread().getName()));        executorService.shutdown();    }}

pool-1-thread-1
pool-1-thread-2
pool-1-thread-3
future------null
pool-1-thread-4
future------我也是Callable
pool-1-thread-5
future------我是Callable
pool-1-thread-1
pool-1-thread-2
pool-1-thread-3
pool-1-thread-4
pool-1-thread-5
pool-1-thread-1
pool-1-thread-2

原创粉丝点击