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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM