简体   繁体   English

如何在ExecutorService或线程中运行的Job中停止或杀死或终止单个线程?

[英]How to Stop Or Kill Or Terminate a Single Thread in ExecutorService or Job runs in the Thread?

a.一种。 How to Stop Or Kill Or Terminate a Single Thread in ExecutorService ?如何停止或终止或终止ExecutorService的单个线程?

b.How to Stop Or Kill Or Terminate the Job which is running in ExecutorService instead of the Thread which is holding the job?如何停止或终止或终止在ExecutorService运行的Job而不是持有作业的线程?

Thanks, Kathir谢谢,凯瑟尔

It depends on what exactly you are doing in you job ( = in your Runnable / Callable -implementation).这取决于您在工作中到底在做什么(= 在您的Runnable / Callable实现中)。 If you are processing a list of items, you could check for a cancel-flag in between (which should be declared as volatile).如果您正在处理项目列表,则可以检查两者之间的取消标志(应声明为 volatile)。

You could also use cancel(true) on your future object (when you are submitting a callable), but this as well would require that you test for Thread.currentThread().isInterrupted() inside your job.您也可以在未来的对象上使用 cancel(true)(当您提交可调用对象时),但这也需要您在工作中测试Thread.currentThread().isInterrupted()

We also tracked the thread via socket programming.我们还通过套接字编程跟踪线程。

Assume a call has been initiated from web app to third party application.假设已从 Web 应用程序发起呼叫到第三方应用程序。

Initiating the request via a thread using socket programming使用套接字编程通过线程发起请求

Whenever user wants to kill the thread, we can do it in socket programming as it holds the exact thread.每当用户想要终止线程时,我们都可以在套接字编程中进行,因为它拥有确切的线程。

In Quartz there is no way we can kill a running thread.在 Quartz 中,我们无法杀死正在运行的线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM