我怎样才能杀死一个线程?不使用 stop();
Thread currentThread=Thread.currentThread();
public void run()
{
while(!shutdown)
{
try
{
System.out.println(currentThread.isAlive());
Thread.interrupted();
System.out.println(currentThread.isAlive());
if(currentThread.isAlive()==false)
{
shutdown=true;
}
}
catch(Exception e)
{
currentThread.interrupt();
}
}
}
});
thread.start();
Thread currentThread=Thread.currentThread();
public void run()
{
while(!shutdown)
{
try
{
System.out.println(currentThread.isAlive());
Thread.interrupted();
System.out.println(currentThread.isAlive());
if(currentThread.isAlive()==false)
{
shutdown=true;
}
}
catch(Exception e)
{
currentThread.interrupt();
}
}
}
});
thread.start();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
调用 stop 的替代方法是使用中断向线程发出信号,表明您希望它完成正在执行的操作。 (这假设您想要停止的线程表现良好,如果它在抛出 InterruptedExceptions 后立即将它们吃掉,并且不检查中断状态,那么您将返回使用 stop() 来忽略 InterruptedExceptions。)
这是我编写的一些代码作为线程问题的答案此处,这是线程中断如何工作的示例:
需要注意的一些事项:
中断会导致
sleep()
和wait()
立即抛出,否则你被困在等待睡眠时间过去。请注意,不需要单独的布尔标志。
被停止的线程通过检查中断状态并在 while 循环之外捕获 InterruptedException(用它来退出循环)来进行配合。中断是可以使用异常进行流量控制的地方,这就是它的重点。
在 catch 块中的当前线程上设置中断从技术上来说是最佳实践,但对于本例来说有点过分了,因为没有其他东西需要设置中断标志。
关于发布的代码的一些观察:
发布的示例不完整,但是将当前线程的引用放在实例变量中似乎是一个坏主意。它将被初始化为创建对象的任何线程,而不是执行 run 方法的线程。如果同一个 Runnable 实例在多个线程上执行,则实例变量大多数时候不会反映正确的线程。
检查线程是否处于活动状态必然会导致 true(除非出现错误,其中 currentThread 实例变量引用了错误的线程),
Thread#isAlive
仅在以下情况下为 false线程已完成执行,它不会仅仅因为被中断而返回 false。调用
Thread#interrupted
将导致清除中断标志,并且在这里没有任何意义,特别是因为返回值被丢弃。调用 Thread#interrupted 的目的是测试中断标志的状态,然后清除它,这是抛出 InterruptedException 的事物使用的一种便捷方法。The alternative to calling stop is to use interrupt to signal to the thread that you want it to finish what it's doing. (This assumes the thread you want to stop is well-behaved, if it ignores InterruptedExceptions by eating them immediately after they are thrown and doesn't check the interrupted status then you are back to using stop().)
Here's some code I wrote as an answer to a threading question here, it's an example of how thread interruption works:
Some things to be aware of:
Interrupting causes
sleep()
andwait()
to immediately throw, otherwise you are stuck waiting for the sleep time to pass.Note that there is no need for a separate boolean flag.
The thread being stopped cooperates by checking the interrupted status and catching InterruptedExceptions outside the while loop (using it to exit the loop). Interruption is one place where it's ok to use an exception for flow control, that is the whole point of it.
Setting interrupt on the current thread in the catch block is technically best-practice but is overkill for this example, because there is nothing else that needs the interrupt flag set.
Some observations about the posted code:
The posted example is incomplete, but putting a reference to the current thread in an instance variable seems like a bad idea. It will get initialized to whatever thread is creating the object, not to the thread executing the run method. If the same Runnable instance is executed on more than one thread then the instance variable won't reflect the right thread most of the time.
The check for whether the thread is alive is necessarily always going to result in true (unless there's an error where the currentThread instance variable is referencing the wrong thread),
Thread#isAlive
is false only after the thread has finished executing, it doesn't return false just because it's been interrupted.Calling
Thread#interrupted
will result in clearing the interrupt flag, and makes no sense here, especially since the return value is discarded. The point of callingThread#interrupted
is to test the state of the interrupted flag and then clear it, it's a convenience method used by things that throwInterruptedException
.通常,线程在中断时终止。那么,为什么不使用原生布尔值呢?尝试 isInterrupted():
Typically, a thread is terminated when it's interrupted. So, why not use the native boolean? Try isInterrupted():
好的方法是使用布尔标志来向线程发出信号。
创建一个名为 myrunnable 的 MyRunnable 实例,将其包装在新的 Thread 实例中并启动该实例。当您想要将线程标记为停止时,请设置 myrunnable.stopThread = true。这样,它就不会在某些事情的中间停止,只会在我们期望它停止的地方停止。
Good way to do it would be to use a boolean flag to signal the thread.
Create a MyRunnable instance called myrunnable, wrap it in a new
Thread
instance and start the instance. When you want to flag the thread to stop, set myrunnable.stopThread = true. This way, it doesn't get stopped in the middle of something, only where we expect it to get stopped.