简体   繁体   English

Java:停止没有同步的线程

[英]Java: stopping a thread with no synchronization

TL;DR Is it OK to a Java thread when I know it isn't synchronized to anything else? TL; DR当我知道Java线程没有与其他任何东西同步时,它是否可以使用它?

First I'll describe my predicament. 首先,我将描述我的困境。 I have a service that handles requests from an app (both on the same machine). 我有一个服务来处理来自应用程序的请求(两者都在同一台机器上)。 This service is single threaded. 这项服务是单线程的。 In some cases, answering requests can take a while. 在某些情况下,回答请求可能需要一段时间。 I'd like to time out the request in those cases. 在这些情况下,我想暂停请求。 Sadly, it's quite complicated to poll interrupts inside said service, as it has quite expansive algorithmic logic, and would cause lots of ugly code rewriting. 遗憾的是,在所述服务中轮询中断非常复杂,因为它具有相当广泛的算法逻辑,并且会导致许多丑陋的代码重写。

Think of the service as a facade that calls a library of scary algorithmic stuff. 将服务视为一个称为可怕算法库的库的外观。 Currently the service simply calls the library upon request, I want it to start a thread, and time it out if computation takes too long. 目前,该服务只是根据请求调用库,我希望它启动一个线程,并在计算时间过长时将其计时。 I intend on using just a single extra thread. 我打算只使用一个额外的线程。

I read How do you kill a thread in Java? 我读过你如何用Java杀死一个线程? and https://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html https://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html

According to these, stopping a thread is deprecated because of loss of synchronization. 根据这些,由于失去同步,不推荐停止线程。 However, my service is basically single threaded, and thus has no need for synchronization. 但是,我的服务基本上是单线程的,因此不需要同步。

In this case, is it OK for me to stop the thread? 在这种情况下,我可以停止线程吗? Am I missing some internal java stuff? 我错过了一些内部java的东西吗?

Thanks! 谢谢!

So you have a service that processes requests, and sometimes that processing can take a while. 因此,您有一个处理请求的服务,有时这种处理可能需要一段时间。 You want to impose some maximum amount of time your service could spend processing that request before giving up. 您想要放弃服务在放弃之前处理该请求所花费的最长时间。

You don't want to litter your code with checks to a flag that would be set by some interrupt, because the code is large and complicated. 您不希望通过检查将某些中断设置的标志丢弃代码,因为代码很大且很复杂。

Your options: 你的选择:

  1. Don't give up. 不要放弃。

Process each request on a separate thread. 在单独的线程上处理每个请求。 Allow each request to run to completion, taking as much time as necessary. 允许每个请求运行完成,并根据需要花费尽可能多的时间。 Consider improving the service request code, perhaps using parallelism, to reduce the amount of time it takes to run. 考虑改进服务请求代码(可能使用并行性),以减少运行所需的时间。

  1. Litter your code with 'time to stop' checks. 使用“停止时间”检查来破坏您的代码。

This is the second best answer - you have controlled access over where your program decides to stop processing the response. 这是第二个最佳答案 - 您可以控制程序决定停止处理响应的位置。 Your program can controllably shut down the request processing, properly cleaning up whatever it needs to (perhaps open resources such as files, sockets, etc; resetting program state internally). 您的程序可以可控制地关闭请求处理,正确清理它需要的任何内容(可能打开文件,套接字等资源;在内部重置程序状态)。

  1. Kill the thread. 杀死线程。

Calling Thread.stop() is deprecated, and for good reason - it can corrupt synchronization state. 不推荐调用Thread.stop() ,并且有充分的理由 - 它可以破坏同步状态。 If your request handling logic uses any form of synchronization, then you'll cause corruption. 如果您的请求处理逻辑使用任何形式的同步,那么您将导致损坏。 Keep in mind that synchronization is used everywhere, even if you don't see it. 请记住,即使您没有看到同步也会在任何地方使用。 Reading from a file uses a form of synchronization. 从文件读取使用同步形式。 Writing to the standard out uses synchronization. 写入标准输出使用同步。

This is possible, but it's a terrible idea. 这是可能的,但这是一个糟糕的主意。

... ...

There are other strategies related to #2 - for instance, you could restructure your program so that each section of processing is created as a job on a queue, that one thread is responsible for dequeuing and executing - basically, the message pump pattern. 还有其他与#2相关的策略 - 例如,您可以重构您的程序,以便将每个处理部分创建为队列中的作业,一个线程负责出列和执行 - 基本上是消息泵模式。 In this design, you'd have a nice choke point at which to put your 'timed out' logic. 在这个设计中,你有一个很好的阻塞点可以放置你的'超时'逻辑。 This is the design used by most windowing frameworks - the UI message pump. 这是大多数窗口框架使用的设计 - UI消息泵。

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

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