日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java高级----Thread之ScheduledExecutorService的使用

發布時間:2024/4/15 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java高级----Thread之ScheduledExecutorService的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

 ScheduledExecutorService的主要作用就是可以將定時任務與線程池功能結合使用。今天我們來學習一下ScheduledExecutorService的用法。我們都太渺小了,那么容易便湮沒于各自的殊途。

?

ScheduledExecutorService的簡單使用

一、使用scheduleAtFixedRate()方法實現周期性執行

public class ScheduledExecutorServiceTest {public static void main(String[] args) {ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();executorService.scheduleAtFixedRate(new Runnable() {@Overridepublic void run() {System.out.println("run "+ System.currentTimeMillis());}}, 0, 100, TimeUnit.MILLISECONDS);} }

?運行的結果如下:立刻執行,而且每隔100毫秒執行一次。

run 1501051231331 run 1501051231427 run 1501051231527 run 1501051231628 run 1501051231726 run 1501051231827 run 1501051231926 run 1501051232026 run 1501051232127 .......

?

二、ScheduledExecutorService使用Callable延遲運行

package com.linux.thread;import java.util.ArrayList; import java.util.List; import java.util.concurrent.*;public class CallableRun {public static void main(String[] args) {try {List<Callable> callableList = new ArrayList<>();callableList.add(new MyCallableA());callableList.add(new MyCallableB());ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();ScheduledFuture futureA = executorService.schedule(callableList.get(0), 4L, TimeUnit.SECONDS);ScheduledFuture futureB = executorService.schedule(callableList.get(1), 4L, TimeUnit.SECONDS);System.out.println(" X = " + System.currentTimeMillis());System.out.println("返回值A:" + futureA.get());System.out.println("返回值B:" + futureB.get());System.out.println(" Y = " + System.currentTimeMillis());executorService.shutdown();} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}}static class MyCallableA implements Callable<String> {@Overridepublic String call() throws Exception{try {System.out.println("callA begin " + Thread.currentThread().getName() + ", " + System.currentTimeMillis());TimeUnit.SECONDS.sleep(3); // 休眠3秒System.out.println("callA end " + Thread.currentThread().getName() + ", " + System.currentTimeMillis());} catch (Exception e) {e.printStackTrace();}return "returnA";}}static class MyCallableB implements Callable<String> {@Overridepublic String call() throws Exception{System.out.println("callB begin " + Thread.currentThread().getName() + ", " + System.currentTimeMillis());System.out.println("callB end " + Thread.currentThread().getName() + ", " + System.currentTimeMillis());return "returnB";}} }

運行的結果如下:

?

三、使用scheduleWithFixedDelay()方法實現周期性執行

package com.linux.thread;import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit;public class RunMain {public static void main(String[] args) {ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();System.out.println(" x = " + System.currentTimeMillis());executorService.scheduleWithFixedDelay(new MyRunable(), 1, 2, TimeUnit.SECONDS);System.out.println(" y = " + System.currentTimeMillis());}static class MyRunable implements Runnable {@Overridepublic void run() {try {System.out.println(" begin = " + System.currentTimeMillis() + ", name: " + Thread.currentThread().getName());TimeUnit.SECONDS.sleep(4);System.out.println(" end = " + System.currentTimeMillis() + ", name: " + Thread.currentThread().getName());} catch (InterruptedException e) {e.printStackTrace();}}} }

運行的結果如下:

?

友情鏈接

  • 可以參考博客:http://blog.csdn.net/cages/article/details/7300992
  • 關于spring中的定時器:http://blog.csdn.net/tsyj810883979/article/details/8481621

轉自:https://www.cnblogs.com/huhx/p/baseusejavaScheduledExecutorService.html

轉載于:https://www.cnblogs.com/zhi-xing/p/10399905.html

總結

以上是生活随笔為你收集整理的java高级----Thread之ScheduledExecutorService的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。