IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

五、Java并发 ScheduledThreadPoolExecutor

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

文章永久连接:https://tech.souyunku.com/?p=5573

ScheduledThreadPoolExecutor 扩展自 一文秒懂 Java 线程池之 ThreadPoolExecutor 讲解的 了ThreadPoolExecutor 类,并且添加了其它方法实现了 ScheduledExecutorService 接口。

  • schedule() 方法允许在指定的延迟后执行一次任务
  • scheduleAtFixedRate() 方法允许在指定的初始延迟后执行任务,然后以一定的周期重复执行,其中 period 参数用于指定两个任务的开始时间之间的间隔时间,因此任务执行的频率是固定的。
  • scheduleWithFixedDelay() 方法类似于 scheduleAtFixedRate() ,它也重复执行给定的任务,但period 参数用于指定前一个任务的结束和下一个任务的开始之间的间隔时间。也就是指定下一个任务延时多久后才执行。执行频率可能会有所不同,具体取决于执行任何给定任务所需的时间。

静态方法 Executors.newScheduledThreadPool() 方法用于创建包含了指定 corePoolSize,无上限 maximumPoolSize0 存活时间 keepAliveTimeScheduledThreadPoolExecutor 实例。

例如下面的示例创建了一个包含了 5 个核心线程的 ScheduledThreadPoolExecutor 实例,且每隔 500 毫秒运行一个输出 Hello World 的任务

ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
executor.schedule(() -> {
    System.out.println("Hello World");
}, 500, TimeUnit.MILLISECONDS);

范例 2

下面的代码则演示了如何在 500 毫秒延迟后执行任务,然后每 100 毫秒重复执行一次。

CountDownLatch lock = new CountDownLatch(3);

ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
ScheduledFuture<?> future = executor.scheduleAtFixedRate(() -> {
    System.out.println("Hello World");
    lock.countDown();
}, 500, 100, TimeUnit.MILLISECONDS);

lock.await(1000, TimeUnit.MILLISECONDS);
future.cancel(true);

干货推荐

本站推荐:精选优质专栏

附录:Java 并发:系列文章


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(96) 打赏



未经允许不得转载:搜云库技术团队 » 五、Java并发 ScheduledThreadPoolExecutor

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367