书上(Java核心技术,卷一,英文版,P728)举了个例子,说,要查看一个线程的“打断状态”是否被设置,首先调用Thread.currentThread(),再调用isInterrupted(),例如:while (!Thread.currentThread().isInterrupted() && more work to do)
{
do more work
}
一个线程可以对打断进行处理决定是否终止,但大多数的线程对于打断的处理应该是终止,这样的线程的run()方法应该如此:
public void run()
{
try
{
. . .
while (!Thread.currentThread().isInterrupted() && more work to do)
{
do more work
}
}
catch(InterruptedException e)
{
// thread was interrupted during sleep or wait
}
finally
{
cleanup, if required
}
// exiting the run method terminates the thread
}
我想问,为什么要调用Thread.currentThread()啊,直接调用isInterrupted()不行吗?因为
while (!Thread.currentThread().isInterrupted() && more work to do)这句不是在run方法里吗,获取了currentThread不也是"this"吗?新手,不解,求教!!
{
do more work
}
一个线程可以对打断进行处理决定是否终止,但大多数的线程对于打断的处理应该是终止,这样的线程的run()方法应该如此:
public void run()
{
try
{
. . .
while (!Thread.currentThread().isInterrupted() && more work to do)
{
do more work
}
}
catch(InterruptedException e)
{
// thread was interrupted during sleep or wait
}
finally
{
cleanup, if required
}
// exiting the run method terminates the thread
}
我想问,为什么要调用Thread.currentThread()啊,直接调用isInterrupted()不行吗?因为
while (!Thread.currentThread().isInterrupted() && more work to do)这句不是在run方法里吗,获取了currentThread不也是"this"吗?新手,不解,求教!!
书上的意思,应该是Tread里的run方法,原文有句话是这样的:The run method
of such a thread has the following form:
public void run()
{
try
......
......
(就是上面我粘的代码)
LZ结帖率这么低,不结的话以后没人回答你的。