线程池

作者: BenjaminCool | 来源:发表于2020-05-28 15:57 被阅读0次


    /**
     * 当网络和代码耗时高, 线程池再多, 一样很快耗尽线程池;
     * 网络和代码耗时稳定, 适度增加线程池数量可提高单位时间内任务处理量;
     */
    private static ExecutorService executorTask = new ThreadPoolExecutor(
            1,
            1,
            100L,
            TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(2),
            new ThreadPoolExecutor.CallerRunsPolicy());


    public static void main(String[] args) throws InterruptedException {

        Callable<String> callable = () -> {
            Thread.sleep(2000L);
            String tname = Thread.currentThread().getName();
            System.out.println(tname);

            return tname;
        };


        ArrayList<Callable<String>> callables = new ArrayList<>();


        for (int i = 0; i < 10; i++) {
            callables.add(callable);
        }

        executorTask.invokeAll(callables);

        executorTask.shutdown();
    }


       /*

      AbstractExecutorService


    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
        throws InterruptedException {
        if (tasks == null)
            throw new NullPointerException();
        ArrayList<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
        boolean done = false;
        try {

          //在main线程循环任务队列
          // CallerRunsPolicy卡住了, 循环任务队列也会被卡主, 都是在main线程执行
         
            for (Callable<T> t : tasks) {
                RunnableFuture<T> f = newTaskFor(t);
                futures.add(f);
                execute(f);
            }
            for (int i = 0, size = futures.size(); i < size; i++) {
                Future<T> f = futures.get(i);
                if (!f.isDone()) {
                    try {
                        f.get();
                    } catch (CancellationException ignore) {
                    } catch (ExecutionException ignore) {
                    }
                }
            }
            done = true;
            return futures;
        } finally {
            if (!done)
                for (int i = 0, size = futures.size(); i < size; i++)
                    futures.get(i).cancel(true);
        }
    }
    
    main线程中循环任务队列
    把任务提交给子线程或任务队列,如果队列满了, 或者没有子线程接收任务, 
    则走拒绝策略, CallerRunsPolicy 在main线程执行子任务, 因为会卡住main线程循环任务队列。
    
    ThreadPoolExecutor

      public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();

       int c = ctl.get();
        if (workerCountOf(c) < corePoolSize) {


        if (addWorker(command, true)) //  子线程中执行任务
            return;
        c = ctl.get();
    }
        if (isRunning(c) && workQueue.offer(command)) {
        int recheck = ctl.get();
        if (! isRunning(recheck) && remove(command))
            reject(command); // main线程中执行任务

        else if (workerCountOf(recheck) == 0)
            addWorker(null, false);
    }
        else if (!addWorker(command, false))
                reject(command);
   }



     */


相关文章

  • java线程池

    线程VS线程池 普通线程使用 创建线程池 执行任务 执行完毕,释放线程对象 线程池 创建线程池 拿线程池线程去执行...

  • java----线程池

    什么是线程池 为什么要使用线程池 线程池的处理逻辑 如何使用线程池 如何合理配置线程池的大小 结语 什么是线程池 ...

  • Java线程池的使用

    线程类型: 固定线程 cached线程 定时线程 固定线程池使用 cache线程池使用 定时调度线程池使用

  • Spring Boot之ThreadPoolTaskExecut

    初始化线程池 corePoolSize 线程池维护线程的最少数量keepAliveSeconds 线程池维护线程...

  • 线程池

    1.线程池简介 1.1 线程池的概念 线程池就是首先创建一些线程,它们的集合称为线程池。使用线程池可以很好地提高性...

  • 多线程juc线程池

    java_basic juc线程池 创建线程池 handler是线程池拒绝策略 排队策略 线程池状态 RUNNIN...

  • ThreadPoolExecutor线程池原理以及源码分析

    线程池流程: 线程池核心类:ThreadPoolExecutor:普通的线程池ScheduledThreadPoo...

  • 线程池

    线程池 [TOC] 线程池概述 什么是线程池 为什么使用线程池 线程池的优势第一:降低资源消耗。通过重复利用已创建...

  • java 线程池使用和详解

    线程池的使用 构造方法 corePoolSize:线程池维护线程的最少数量 maximumPoolSize:线程池...

  • 线程池

    JDK线程池 为什么要用线程池 线程池为什么这么设计 线程池原理 核心线程是否能被回收 如何回收空闲线程 Tomc...

网友评论

      本文标题:线程池

      本文链接:https://www.haomeiwen.com/subject/dadaahtx.html