⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 quartzschedulerthread.java

📁 定时器开源项目, 相对于 jcrontab, Quartz 算是更完整的一个项目, 随著开发的版本上来, 他已经脱离只是写在程序里面的计时器, 在指定的时间或区间, 处理所指定的事件. 也加入了 se
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright James House (c) 2001-2004 *  * All rights reserved. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: 1. * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. 2. Redistributions in * binary form must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. *  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package org.quartz.core;import java.util.Random;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.quartz.Job;import org.quartz.JobPersistenceException;import org.quartz.SchedulerException;import org.quartz.Trigger;import org.quartz.spi.TriggerFiredBundle;/** * <p> * The thread responsible for performing the work of firing <code>{@link Trigger}</code> * s that are registered with the <code>{@link QuartzScheduler}</code>. * </p> *  * @see QuartzScheduler * @see Job * @see Trigger *  * @author James House */public class QuartzSchedulerThread extends Thread {    /*     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     *      * Data members.     *      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     */    private QuartzScheduler qs;    private QuartzSchedulerResources qsRsrcs;    private Object pauseLock = new Object();    private Object idleLock = new Object();    private boolean signaled;    private boolean paused;    private boolean halted;    private SchedulingContext ctxt = null;    private Random random = new Random(System.currentTimeMillis());    // When the scheduler finds there is no current trigger to fire, how long    // it should wait until checking again...    private static long DEFAULT_IDLE_WAIT_TIME = 30L * 1000L;    private long idleWaitTime = DEFAULT_IDLE_WAIT_TIME;    private int idleWaitVariablness = 7 * 1000;    private long dbFailureRetryInterval = 15L * 1000L;    /*     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     *      * Constructors.     *      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     */    /**     * <p>     * Construct a new <code>QuartzSchedulerThread</code> for the given     * <code>QuartzScheduler</code> as a non-daemon <code>Thread</code>     * with normal priority.     * </p>     */    QuartzSchedulerThread(QuartzScheduler qs, QuartzSchedulerResources qsRsrcs,            SchedulingContext ctxt) {        this(qs, qsRsrcs, ctxt, false, Thread.NORM_PRIORITY);    }    /**     * <p>     * Construct a new <code>QuartzSchedulerThread</code> for the given     * <code>QuartzScheduler</code> as a <code>Thread</code> with the given     * attributes.     * </p>     */    QuartzSchedulerThread(QuartzScheduler qs, QuartzSchedulerResources qsRsrcs,            SchedulingContext ctxt, boolean setDaemon, int threadPrio) {        super(qs.getSchedulerThreadGroup(), qsRsrcs.getThreadName());        this.qs = qs;        this.qsRsrcs = qsRsrcs;        this.ctxt = ctxt;        this.setDaemon(setDaemon);        this.setPriority(threadPrio);        // start the underlying thread, but put this object into the 'paused'        // state        // so processing doesn't start yet...        paused = true;        halted = false;        this.start();    }    /*     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     *      * Interface.     *      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     */    void setIdleWaitTime(long waitTime) {        idleWaitTime = waitTime;        idleWaitVariablness = (int) (waitTime * 0.2);    }    private long getDbFailureRetryInterval() {        return dbFailureRetryInterval;    }    public void setDbFailureRetryInterval(long dbFailureRetryInterval) {        this.dbFailureRetryInterval = dbFailureRetryInterval;    }    private long getRandomizedIdleWaitTime() {        return idleWaitTime - random.nextInt(idleWaitVariablness);    }    /**     * <p>     * Signals the main processing loop to pause at the next possible point.     * </p>     */    void togglePause(boolean pause) {        synchronized (pauseLock) {            paused = pause;            if (paused) {                signalSchedulingChange();            } else {                pauseLock.notify();            }        }    }    /**     * <p>     * Signals the main processing loop to pause at the next possible point.     * </p>     */    void halt() {        synchronized (pauseLock) {            halted = true;            if (paused) {                pauseLock.notify();            } else {                signalSchedulingChange();            }        }    }    boolean isPaused() {        return paused;    }    /**     * <p>     * Signals the main processing loop that a change in scheduling has been     * made - in order to interrupt any sleeping that may be occuring while     * waiting for the fire time to arrive.     * </p>     */    void signalSchedulingChange() {        signaled = true;    }    /**     * <p>     * The main processing loop of the <code>QuartzSchedulerThread</code>.     * </p>     */    public void run() {        boolean lastAcquireFailed = false;                while (!halted) {            // check if we're supposed to pause...            synchronized (pauseLock) {                while (paused && !halted) {                    try {                        // wait until togglePause(false) is called...                        pauseLock.wait(100L);                    } catch (InterruptedException ignore) {                    }                }                if (halted) {                    break;                }            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -