ExecutorService

ExecutorService 即线程池。

 

1 submit

 提交一个返回值的任务用于执行,返回一个表示任务的未决结果的 Future

2 shutdown


 启动一次顺序关闭,执行以前提交的任务,但不接受新任务.

3 shutdownNow

试图停止所有正在执行的活动任务,暂停处理正在等待的任务,并返回等待执行的任务列表

4 isShutdown

如果此执行程序已关闭,则返回 true。

5 isTerminated

如果关闭后所有任务都已完成,则返回 true。除非首先调用 shutdown 或 shutdownNow,否则 isTerminated 永不为 true

6 awaitTermination

这个方法有两个参数,一个是timeout即超时时间,另一个是unit即时间单位。这个方法会使线程等待timeout时长,当超过timeout时间后,会监测ExecutorService是否已经关闭,若关闭则返回true,否则返回false。一般情况下会和shutdown方法组合使用。(awaitTermination不会关闭ExecutorService,只是定时检测一下他是否关闭) 

@Test
    public void test11() throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        CallableTask task1 = new CallableTask(1000);
        CallableTask task2 = new CallableTask(2000);
        CallableTask task3 = new CallableTask(3000);
        CallableTask task4 = new CallableTask(4000);
        CallableTask task5 = new CallableTask(10000);
        executorService.submit(task1);
        executorService.submit(task2);
        executorService.submit(task3);
        executorService.submit(task4);
        executorService.submit(task5);
        executorService.shutdown();
        System.out.println("shutdown exec");
        while (!executorService.awaitTermination(1,TimeUnit.SECONDS)){
            System.out.println("线程池未关闭");
        }
        System.out.println("线程池关闭");
    }

class CallableTask implements Callable{
        Integer time ;
        public CallableTask (Integer time){
            this.time = time;
        }
        @Override
        public Object call() throws Exception {
            Thread.currentThread().setName("thread" + time );
            System.out.println(Thread.currentThread().getName() + "exec start");
            Thread.sleep(time);
            System.out.println(Thread.currentThread().getName() + "exec end");
            return time;
        }
    }

结果:可以看到awaitTermination每秒执行一次

shutdown exec
thread4000exec start
thread2000exec start
thread1000exec start
thread3000exec start
thread10000exec start
thread1000exec end
线程池未关闭
thread2000exec end
线程池未关闭
thread3000exec end
线程池未关闭
thread4000exec end
线程池未关闭
线程池未关闭
线程池未关闭
线程池未关闭
线程池未关闭
线程池未关闭
thread10000exec end
线程池关闭

7 invokeAll

执行给定的任务,当所有任务完成时,返回保持任务状态和结果的 Future 列表。如果设置了超时时间,当超时后,会将为完成的任务中断,并返回结果的future列表

@Test
    public void test10() throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        CallableTask task1 = new CallableTask(1000);
        CallableTask task2 = new CallableTask(2000);
        CallableTask task3 = new CallableTask(3000);
        CallableTask task4 = new CallableTask(4000);
        CallableTask task5 = new CallableTask(10000);
        List> callables = new ArrayList();
        callables.add(task1);
        callables.add(task2);
        callables.add(task3);
        callables.add(task4);
        callables.add(task5);
        List> futures = executorService.invokeAll(callables,2,TimeUnit.SECONDS);
        futures.stream().forEach(feature->{
            try {
                Integer integer = feature.get();
                System.out.println(integer);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });
    }

8 invokeAny

执行给定的任务,如果某个任务已成功完成(也就是未抛出异常),则返回其结果。一旦正常或异常返回后,则取消尚未完成的任务。如果此操作正在进行时修改了给定的 collection,则此方法的结果是不确定的

你可能感兴趣的:(Java)