如何解决在Java中,是否有办法检查Runnable是否在单线程中完成?
在Java中,我需要获取两个参数,即Runnable和Delay(以毫秒为单位),并且需要在延迟时间内运行Runnable。这需要在单线程中运行,并且如果使用不同的参数值调用该方法并且上一个任务尚未完成,则应将其保存在队列中。
public void runScheduledTask(Runnable task,long delay) {
// ...
}
...
runScheduledTask(task1,10); // at 00:00:00.000
runScheduledTask(task2,10); // at 00:00:00.005
当使用task2调用该方法时,由于延迟10,task1尚未启动/结束,因此task2应该存储在队列中。 在这种情况下,有什么方法可以检查task1是否已完成? 由于这应该在当前线程中运行,因此我不知道可以使用哪些类或工具。
解决方法
tl; dr
有没有一种方法可以检查Runnable是否在单线程中完成?
是的
- 使用
Runnable
调用提供的ScheduledExecutorService
运行Executors.newSingleThreadScheduledExecutor
。 - 捕获返回的
ScheduledFuture
对象。 - 询问该
ScheduledFuture
对象的完成状态,询问其完成还是被取消。
执行程序框架
现代Java提供了Executors框架来很好地管理将任务分解到其他线程上。参见tutorial by Oracle。
Executors
类(名称多个)具有用于生成ExecutorService
对象的工厂方法。
您要延迟执行而不是立即执行。因此,这意味着您想要一个ScheduledExecutorService
。
ScheduledExecutorService scheduledExecutorService = …
您要求单线程执行。这意味着您想要一个由thread pool支持的执行程序,其大小为单个线程而不是多个线程。
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor() ;
告诉计划的执行人服务,在您期望的延迟后运行您的Runnable
。调用ScheduledExecutorService::schedule
方法。
如果今天正在编写执行者框架,则延迟将表示为java.time.Duration
对象。但是 java.time 尚未被发明。因此,延迟用TimeUnit
对象表示为某个数字,以指定时间的粒度。您没有指定所需的粒度,因此我将花几秒钟。
scheduledExecutorService.schedule( runnable,10,TimeUnit.SECONDS );
将所有内容放在一起。
Runnable runnable = ( ) -> {
System.out.println( "Running the runnable at " + Instant.now() );
};
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.schedule( runnable,TimeUnit.SECONDS ); // Wait ten seconds before running the runnable.
// Wait long enough on this `main` thread for the background thread to do its thing.
try
{
Thread.sleep( Duration.ofSeconds( 12 ).toMillis() );
}
catch ( InterruptedException e )
{
e.printStackTrace();
}
// Clean-up.
scheduledExecutorService.shutdown(); // Always shutdown your executor service when no longer needed,or when ending your app.
System.out.println( "Done at " + Instant.now() );
注意我们如何正常关闭执行程序服务。否则,支持执行程序的线程可能会在我们的应用程序结束后无限期地继续运行类似僵尸的程序。
请参阅此code run live at IdeOne.com。
Running the runnable at 2020-11-16T05:30:04.106661Z
Done at 2020-11-16T05:30:06.105624Z
检查完成情况
您问:
有没有一种方法可以检查task1是否已完成
是的
上面看到的ScheduledExecutorService::schedule
方法返回一个ScheduledFuture
对象。我们可以查询返回的ScheduledFuture
对象的完成状态。
我们上面的代码忽略了返回的对象。让我们更改代码以捕获返回的对象。
Runnable runnable = ( ) -> {
System.out.println( "Running the runnable at " + Instant.now() );
};
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
ScheduledFuture future = scheduledExecutorService.schedule( runnable,TimeUnit.SECONDS ); // Wait ten seconds before running the runnable.
try
{
Thread.sleep( Duration.ofSeconds( 4 ).toMillis() ); // Wait a short while,then see if runnable completed.
System.out.println( "isDone: " + future.isDone() + " | " + "isCancelled: " + future.isCancelled() );
Thread.sleep( Duration.ofSeconds( 4 ).toMillis() ); // Wait a short while,then see if runnable completed.
System.out.println( "isDone: " + future.isDone() + " | " + "isCancelled: " + future.isCancelled() );
}
catch ( InterruptedException e )
{
e.printStackTrace();
}
// Clean-up.
scheduledExecutorService.shutdown();
System.out.println( "Done at " + Instant.now() );
运行时。
isDone: false | isCancelled: false
isDone: false | isCancelled: false
Running the runnable at 2020-11-16T05:44:07.803125Z
isDone: true | isCancelled: false
Done at 2020-11-16T05:44:09.821267Z
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。