如果需要更多时间,则终止 threadexecutor 中的任务
场景如下:
我有一个 java 主进程,它使用 JMS 发布到某个 ActiveMQ 代理。
每次应该将消息发送到代理时,都会使用固定大小的线程池(使用 ThreadExecutor)中的一个线程,并在其中进行发布调用。
现在,发布调用是阻塞调用,如果代理关闭,线程本身将继续等待。
我想创建一个线程池,这样如果特定线程在 X 时间内没有完成任务,它只会返回,即如果代理已关闭且发布未完成,则线程不会继续等待,而是返回到游泳池。
目前还没有办法使这个发布调用异步,所以我猜处理这种情况的唯一方法就是上面提到的方法。
如果线程无法在给定的时间范围内完成任务,是否有任何 ThreadExecutor 允许我立即终止线程?
希望有人提供更优雅的解决方案。
Here is the scenario:
I have a java main process, which uses JMS to publish to some ActiveMQ broker.
Each time a message is supposed to be sent to the broker, a thread is used from a fixed sized thread pool (using ThreadExecutor) and inside it the publish calls are made.
Now , a publish call is a blocking call and if the broker is down , the thread itself keeps waiting.
I want to create a threadpool such that if a specific thread is not over with the task in X amount of time, it simply returns i.e if the broker is down and the publish does not go through, the thread does not keep waiting and instead return to the pool.
There is no way as of now to make this publish call Asynchronous, so the only way to handle this situation is the one mentioned above I guess.
Are there any ThreadExecutors out there which allow me to terminate a thread immidiately, if the thread is not able to complete a task in a given timeframe??
Would love a more elegant solution from someone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ExecutorService
接口提供带有timeout
参数的invokeAll()
和invokeAny()
方法。例子:
The
ExecutorService
interface offers the methodsinvokeAll()
andinvokeAny()
with atimeout
parameter.Example: