java线程延迟执行_java – 更改线程池中一个线程的延迟

博客给出Java线程延迟执行的完整有效示例,使用ScheduledExecutorService安排三个任务按不同间隔执行,之后对第二个任务进行重调度,将其执行间隔从10秒改为20秒,且不影响其他任务,还展示了相应输出结果。

你完全按照你的方式完成 – 你的代码中肯定会有其他错误.

这是一个完整的,有效的例子,可以做同样的事情.下面的输出显示它正在按预期工作.

public class Reschedule {

public static void main(String[] args) throws InterruptedException {

long start = System.currentTimeMillis();

ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

// Schedule three tasks

ScheduledFuture future1 = executor.scheduleAtFixedRate(() -> {

System.out.printf("%03ds: This is the first runnable, reporting in%n", (System.currentTimeMillis() - start) / 1000);

}, 0, 5, TimeUnit.SECONDS);

ScheduledFuture future2 = executor.scheduleAtFixedRate(() -> {

System.out.printf("%03ds: This is the second runnable, reporting in%n", (System.currentTimeMillis() - start) / 1000);

}, 2, 10, TimeUnit.SECONDS);

ScheduledFuture future3 = executor.scheduleAtFixedRate(() -> {

System.out.printf("%03ds: This is the third runnable, reporting in%n", (System.currentTimeMillis() - start) / 1000);

}, 5, 15, TimeUnit.SECONDS);

// Wait some

Thread.sleep(30000);

// Reschedule the second task

System.out.printf("%03ds: Rescheduling the second runnable to run at 20 second intervals%n", (System.currentTimeMillis() - start) / 1000);

future2.cancel(true);

future2 = executor.scheduleAtFixedRate(() -> {

System.out.printf("%03ds: This is the second runnable, reporting in%n", (System.currentTimeMillis() - start) / 1000);

}, 2, 20, TimeUnit.SECONDS);

}

}

正如您在下面的输出重新安排中看到的那样,第二个任务将重置它并将其从每10秒运行一次变为每20秒,并且第一个和第三个任务不受影响.

输出:

000s: This is the first runnable, reporting in

002s: This is the second runnable, reporting in

005s: This is the first runnable, reporting in

005s: This is the third runnable, reporting in

010s: This is the first runnable, reporting in

012s: This is the second runnable, reporting in

015s: This is the first runnable, reporting in

020s: This is the first runnable, reporting in

020s: This is the third runnable, reporting in

022s: This is the second runnable, reporting in

025s: This is the first runnable, reporting in

030s: This is the first runnable, reporting in

030s: Rescheduling the second runnable to run at 20 second intervals

032s: This is the second runnable, reporting in

035s: This is the first runnable, reporting in

035s: This is the third runnable, reporting in

040s: This is the first runnable, reporting in

045s: This is the first runnable, reporting in

050s: This is the first runnable, reporting in

050s: This is the third runnable, reporting in

052s: This is the second runnable, reporting in

055s: This is the first runnable, reporting in

060s: This is the first runnable, reporting in

065s: This is the first runnable, reporting in

065s: This is the third runnable, reporting in

070s: This is the first runnable, reporting in

072s: This is the second runnable, reporting in

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值