線程池(5)Executors.newScheduledThreadPool

來源:互聯網
上載者:User

標籤:inter   5.5   try   因此   seconds   style   exception   總結   thread   

例子1(scheduleAtFixedRate):延遲2秒後,每隔3秒執行1次

ScheduledExecutorService es = Executors.newScheduledThreadPool(5);        log.info("開始時間");        Runnable syncRunnable = new Runnable() {            @Override            public void run() {                log.info(Thread.currentThread().getName());            }        };        es.scheduleAtFixedRate(syncRunnable, 2000, 3000, TimeUnit.MILLISECONDS);

運行結果:

            10:49:22.495 開始時間            10:49:24.500 pool-1-thread-1            10:49:27.499 pool-1-thread-1            10:49:30.500 pool-1-thread-2            10:49:33.500 pool-1-thread-1            10:49:36.501 pool-1-thread-3            10:49:39.500 pool-1-thread-2            10:49:42.500 pool-1-thread-2            10:49:45.500 pool-1-thread-1            10:49:48.501 pool-1-thread-1            10:49:51.501 pool-1-thread-1            10:49:54.501 pool-1-thread-4            10:49:57.501 pool-1-thread-2            10:50:00.502 pool-1-thread-2            10:50:03.502 pool-1-thread-3            10:50:06.502 pool-1-thread-3            10:50:09.502 pool-1-thread-3            10:50:12.503 pool-1-thread-5            10:50:15.503 pool-1-thread-5            10:50:18.503 pool-1-thread-5            10:50:21.503 pool-1-thread-5            10:50:24.503 pool-1-thread-3            10:50:27.503 pool-1-thread-2            10:50:30.503 pool-1-thread-1            10:50:33.504 pool-1-thread-4            10:50:36.504 pool-1-thread-5            10:50:39.504 pool-1-thread-5            10:50:42.504 pool-1-thread-2

例子2(scheduleWithFixedDelay):延遲5秒後,每個任務執行完後延遲3秒在執行1次

ScheduledExecutorService es = Executors.newScheduledThreadPool(5);        log.info("開始時間");        Runnable syncRunnable = new Runnable() {            @Override            public void run() {                log.info(Thread.currentThread().getName());                try {                    Thread.sleep(1000);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        };        es.scheduleWithFixedDelay(syncRunnable, 5000, 3000, TimeUnit.MILLISECONDS);

運行結果:

            11:10:27.773 開始時間            11:10:32.778 pool-1-thread-1            11:10:36.779 pool-1-thread-1            11:10:40.780 pool-1-thread-2            11:10:44.781 pool-1-thread-1            11:10:48.783 pool-1-thread-3            11:10:52.785 pool-1-thread-3            11:10:56.785 pool-1-thread-4            11:11:00.787 pool-1-thread-4            11:11:04.788 pool-1-thread-4            11:11:08.789 pool-1-thread-4            11:11:12.790 pool-1-thread-3            11:11:16.792 pool-1-thread-1

本來是每隔3秒執行的,但是,由於某個任務處理時間過長,導致延後。本例是延後1秒,即4秒。

 

總結:scheduleAtFixedRate與scheduleWithFixedDelay區別

scheduleAtFixedRate:不管任務是否執行完了,在3秒內必須執行
scheduleWithFixedDelay:等任務執行完了,在等3秒後執行
因此,scheduleWithFixedDelay 非常有用。

 

線程池(5)Executors.newScheduledThreadPool

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.