本文介绍了使用Java的SwingWorker超时完成任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用程序中实现 SwingWorker 类。有没有办法设置一段时间,之后,SwingWorker超时?我想可能会抛出一个 OutOfTime 异常,我可以捕获然后处理。我只是不确定如何实现它。

I am trying to implement a SwingWorker class within my application. Is there a way to set a length of time that after which, the SwingWorker "times out"? I was thinking that maybe throwing an OutOfTime exception that I can catch and then deal with. I'm just not sure how to implement it.

感谢您的帮助!

推荐答案

为什么不将您的任务嵌入Runnable中,将其放入新的单线程然后对生成的。这将为您提供超时功能,因为 get()将在作业未及时完成时抛出异常。

Why not embed your task within a Runnable, drop it into a new single-threaded ExecutorService and then perform a get() on the resulting Future with an appropriate timeout. That would give you your timeout functionality, since get() will throw an exception if the job doesn't complete in time.

这篇关于使用Java的SwingWorker超时完成任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 09:11