quartz-源碼理解第二天

來源:互聯網
上載者:User

1、quartz源碼底層,是使用wait、notifyAll來處理的。所以先要理解清楚wait和notify才可以哦。

先來一個面試常問到的問題:wait和sleep對比,對於兩者,簡而言之sleep是Thread(線程)靜態方法,線程阻塞不釋放擷取的鎖;waits 是Object的方法,同樣會阻塞線程,但是同時會釋放鎖。wait的線程被喚醒有以下情境:a、別的線程調用該對象的notify/all;b、wait 逾時;c、調用interrupted方法。

wait、sleep 比對代碼如下:

public class WaitTest implements Runnable {    int number = 10;    public void firstMethod() throws Exception {        System.out.println("***first begin**"+Thread.currentThread().getName());        synchronized (this) {            System.out.println("***first has get lock**");            number += 100;            System.out.println(number);            Thread.sleep(50000);            this.notify();        }    }    public void secondMethod() throws Exception {        System.out.println("***second begin**"+Thread.currentThread().getName());        synchronized (this) {            System.out.println("***second has get lock**");            /**             * (休息2S,阻塞線程) 以驗證當前線程對象的機鎖被佔用時, 是否被可以訪問其他同步代碼塊             */            // Thread.sleep(2000);            this.wait();            number *= 200;            System.out.println("***second end**" + number+" "+Thread.currentThread().getName());        }    }    @Override    public void run() {        try {            firstMethod();        } catch (Exception e) {            e.printStackTrace();        }    }    public static void main(String[] args) throws Exception {     Thread.currentThread().setName("test_wait_thread");        WaitTest threadTest = new WaitTest();        Thread thread = new Thread(threadTest);        thread.start();        threadTest.secondMethod();    }}

2、quartz實現方案:

常規線程判斷job線程池中有空閑線程,將空閑線程用來執行將要執行的job,(根據下次執行時間和優先順序兩個維度來判斷哪個job先執行)。

missfire線程,判斷是否有miss的觸發器,根據設定的miss策略,決定是否是放棄還是立即執行job


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.