English 中文(简体)
B. 停下来的问题
原标题:Problem with while stop thread

我正试图停止read。 在阻止我的read子时, exception子被打断。

如果read然放弃中断的例外,我可以做些什么。 难道应该赶上它,什么东西或做什么?

问题回答

你们不应只是停止read。 我做的事情大多是在Thread的阶级中制造一种公众(或许是静态的)变数,表明read子何时应当停止。 因此,就像声明一样。

public volatile bool shouldStop = false;

然后,在你read子的每个周期结束时,你可以检查你是否需要放弃(从 while或 something起)。

read可以非常 an忙地处理! 仅仅在表面上要求中断/停止功能是可能的,但大多是不必要的。

There is a reason .stop() and .suspend() were deprecated and should not be used. This article is relevant: http://download.oracle.com/javase/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html

我也引用 Java:

Deprecated. This method is inherently unsafe. Stopping a thread with Thread.stop causes it to unlock all of the monitors that it has locked (as a natural consequence of the unchecked ThreadDeath exception propagating up the stack). If any of the objects previously protected by these monitors were in an inconsistent state, the damaged objects become visible to other threads, potentially resulting in arbitrary behavior. Many uses of stop should be replaced by code that simply modifies some variable to indicate that the target thread should stop running. The target thread should check this variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop running. If the target thread waits for long periods (on a condition variable, for example), the interrupt method should be used to interrupt the wait. For more information, see Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?.

你们应当找到某种方式(无论是某些共同变量还是其他方式)来nch笑你的read子,以便read子能够结束。

这取决于你们的需要。 例如,你可以(例如)这样做:

public void run() {
    try {
         // do thread stuff
    } catch(ThreadInterruptedException ex) {
         // close gracefully what needed to be closed
    }
}

但是,, 停用。 因此,一个更好的解决办法是,将一些蓝色变数用于说明胎面是否应当停止,并提供一种改变的方法,以阻止胎面(见)。 例如,问题





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签