美文网首页
线程池如何重用现有线程

线程池如何重用现有线程

作者: 木木_bfe8 | 来源:发表于2019-04-11 10:49 被阅读0次

声明:此文章是从各个博客整理过来的,如有侵权请留言。

Java的Executors类和newCachedThreadPool( )方法,根据API,生成的线程池将重新使用现有Thread对象进行新任务。

这是如何实现的,因为我无法在API中找到任何方法来设置现有对象的行为Thread。

例如,可以从一个Runnable对象创建一个新的 Thread,这使得Thread调用Runnable的run( )方法。但是,API中没有使用setter方法Runnable作为参数。

看关于ThreadPoolExecutor参数时,看到了keepaliveTime这个参数,这个参数的意思是:“当线程数大于CorePoolSize时,如果有没有等到新的Task,到了keepaliveTime时间后,就自动终止掉”。那么如果在这个时间之前,等到了新的Task,就可以重用这个线程。到底是怎么重用线程的呢?

线程重用的核心是,我们知道,Thread.start()只能调用一次,一旦这个调用结束,则该线程就到了stop状态,不能再次调用start。
则要达到复用的目的,则必须从Runnable接口的run()方法上入手,可以这样设计这个Runnable.run()方法(就叫外面的run()方法):
它本质上是个无限循环,跑的过程中不断检查我们是否有新加入的子Runnable对象(就叫内部的runnable:run()吧,它就是用来实现我们自己的任务),有就调一下我们的run(),其实就一个大run()把其它小run()#1,run()#2,...给串联起来了,基本原理就这么简单
不停地处理我们提交的Runnable任务。

public void run() {
    while(true) {
        if(tasks available) {
           Runnable task = taskqueue.dequeue();
           task.run();
        } else {
           // wait or whatever
        }
    }
}

jdk节选

  /**
     * Main worker run loop.  Repeatedly gets tasks from queue and
     * executes them, while coping with a number of issues:
     *
     * 1. We may start out with an initial task, in which case we
     * don't need to get the first one. Otherwise, as long as pool is
     * running, we get tasks from getTask. If it returns null then the
     * worker exits due to changed pool state or configuration
     * parameters.  Other exits result from exception throws in
     * external code, in which case completedAbruptly holds, which
     * usually leads processWorkerExit to replace this thread.
     *
     * 2. Before running any task, the lock is acquired to prevent
     * other pool interrupts while the task is executing, and then we
     * ensure that unless pool is stopping, this thread does not have
     * its interrupt set.
     *
     * 3. Each task run is preceded by a call to beforeExecute, which
     * might throw an exception, in which case we cause thread to die
     * (breaking loop with completedAbruptly true) without processing
     * the task.
     *
     * 4. Assuming beforeExecute completes normally, we run the task,
     * gathering any of its thrown exceptions to send to afterExecute.
     * We separately handle RuntimeException, Error (both of which the
     * specs guarantee that we trap) and arbitrary Throwables.
     * Because we cannot rethrow Throwables within Runnable.run, we
     * wrap them within Errors on the way out (to the thread's
     * UncaughtExceptionHandler).  Any thrown exception also
     * conservatively causes thread to die.
     *
     * 5. After task.run completes, we call afterExecute, which may
     * also throw an exception, which will also cause thread to
     * die. According to JLS Sec 14.20, this exception is the one that
     * will be in effect even if task.run throws.
     *
     * The net effect of the exception mechanics is that afterExecute
     * and the thread's UncaughtExceptionHandler have as accurate
     * information as we can provide about any problems encountered by
     * user code.
     *
     * @param w the worker
     */
    final void runWorker(Worker w) {
        Thread wt = Thread.currentThread();
        Runnable task = w.firstTask;
        w.firstTask = null;
        w.unlock(); // allow interrupts
        boolean completedAbruptly = true;
        try {
            while (task != null || (task = getTask()) != null) {
                w.lock();
                // If pool is stopping, ensure thread is interrupted;
                // if not, ensure thread is not interrupted.  This
                // requires a recheck in second case to deal with
                // shutdownNow race while clearing interrupt
                if ((runStateAtLeast(ctl.get(), STOP) ||
                     (Thread.interrupted() &&
                      runStateAtLeast(ctl.get(), STOP))) &&
                    !wt.isInterrupted())
                    wt.interrupt();
                try {
                    beforeExecute(wt, task);
                    Throwable thrown = null;
                    try {
                        task.run();
                    } catch (RuntimeException x) {
                        thrown = x; throw x;
                    } catch (Error x) {
                        thrown = x; throw x;
                    } catch (Throwable x) {
                        thrown = x; throw new Error(x);
                    } finally {
                        afterExecute(task, thrown);
                    }
                } finally {
                    task = null;
                    w.completedTasks++;
                    w.unlock();
                }
            }
            completedAbruptly = false;
        } finally {
            processWorkerExit(w, completedAbruptly);
        }
    }

相关文章

  • 线程池如何重用现有线程

    声明:此文章是从各个博客整理过来的,如有侵权请留言。 Java的Executors类和newCachedThrea...

  • Java并发——线程池ThreadPoolExecutor

    线程池作用 相对于为每个请求都创建一个线程,线程池通过重用现有的线程而不是创建新线程,可以在处理多个请求时分摊在线...

  • 线程池

    线程池 项目文件:HelloJava-ThreadPoolExecutorDemo 线程池优点:重用线程,避免创建...

  • Android 线程池原理

    线程池核心类 : ThreadPoolExecutor:提供了一系列参数来配置线程池 线程池优点: 1.重用线程池...

  • java多线程学习5

    线程池 ThreadPoolExecutor 官方API解释线程池的好处: (1)通过重用线程池中的线程,来减少每...

  • 25. 并发终结之ThreadPoolExecutor

    线程池概述 线程池的好处: 线程可重用,线程是稀缺资源,使用线程池可减少创建和销毁线程的次数,每个工作线程都可以被...

  • 池技术

    线程池 优点 重用线程,减少创建和销毁线程的开销(时间,资源)防止线程相互竞争资源 线程池类型 newFixedT...

  • 一篇文章搞懂线程池

    线程池 什么使用使用线程池? 单个任务处理时间比较短 需要处理的任务数量很大 线程池优势 重用存在的线程,减少线程...

  • Android中的线程池

    为什么要使用线程池?Android 中的线程池有哪些?它们的区别是什么? 一、线程池的作用 重用线程池中的线程,可...

  • java Executor

    一、什么是Executor框架? 我们知道线程池就是线程的集合,线程池集中管理线程,以实现线程的重用,降低资源消耗...

网友评论

      本文标题:线程池如何重用现有线程

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