quartz 中的线程池

书接上回:https://www.cnblogs.com/silenceshining/p/15390887.html

定时器要调度多个定时任务,就得有一个线程池来进行任务的并发处理,那来看下quartz中的线程池情况。

 SchedulerFactory schedulerFactory = new StdSchedulerFactory();
 Scheduler scheduler = schedulerFactory.getScheduler();

当执行schedulerFactory.getScheduler()时,会初始化一个线程池SimpleThreadPool,过程如下:

// Get ThreadPool Properties
        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        String tpClass = cfg.getStringProperty(PROP_THREAD_POOL_CLASS, SimpleThreadPool.class.getName());

        if (tpClass == null) {
            initException = new SchedulerException(
                    "ThreadPool class not specified. ");
            throw initException;
        }

        try {
            tp = (ThreadPool) loadHelper.loadClass(tpClass).newInstance();
        } catch (Exception e) {
            initException = new SchedulerException("ThreadPool class '"
                    + tpClass + "' could not be instantiated.", e);
            throw initException;
        }

SimpleThreadPool是一个比较简单的线程池实现,只有线程数这一个属性,不像其他功能比较丰富的线程池有像核心线程数、最大线程数、队列大小等参数。

默认线程数为10,实际运行起来的情况如下截图:

 那如何修改线程数呢?

可采用如下方式:

 Properties prop = new Properties();
        // 线程池配置
        prop.put("org.quartz.threadPool.threadCount", "20");
        SchedulerFactory schedulerFactory = new StdSchedulerFactory(prop);
        Scheduler scheduler = schedulerFactory.getScheduler();

那如何定义一个功能比较丰富的线程池给quartz使用呢(当然很少这样哈)

以下为网上看到的一个参考实现,连接如下,感谢分享:https://gist.github.com/jarek-przygodzki/7991992

原文地址:https://www.cnblogs.com/silenceshining/p/15769066.html