Java Runnablerun()中的Thread.currentThread().interrupt()与this.interrupt()的比较
我需要弄清楚为什么我在互联网上找到的每个关于如何从Runnablerun方法捕获中断的示例都是这样的:Java Runnablerun()中的Thread.currentThread().interrupt()与this.interrupt()的比较,java,multithreading,Java,Multithreading,我需要弄清楚为什么我在互联网上找到的每个关于如何从Runnablerun方法捕获中断的示例都是这样的: while (Thread.currentThread().isInterrupted()) { //foo(); try { Thread.sleep(1000); } catch(InterruptingException e) { Thread.currentThread().interrupt(); } //foo
while (Thread.currentThread().isInterrupted()) {
//foo();
try {
Thread.sleep(1000);
} catch(InterruptingException e) {
Thread.currentThread().interrupt();
}
//foo();
}
我现在对线程的理解已经够多了,我想是的,我不明白为什么如果我们在run方法内部,这不意味着我们可以用这个替换Thread.currentThread吗?这个内部Runnablerun指的是Runnable对象,而不是封闭的Thread对象。在本例中,Thread.currentThread!=这个
但是,如果像下面这样创建线程对象,则不鼓励:
Thread thread = new Thread() {
@Override
public void run() {
System.out.println(Thread.currentThread() == this);
}
};
然后Thread.currentThread==this。此内部Runnablerun指的是可运行对象,而不是封闭的线程对象。在本例中,Thread.currentThread!=这个
但是,如果像下面这样创建线程对象,则不鼓励:
Thread thread = new Thread() {
@Override
public void run() {
System.out.println(Thread.currentThread() == this);
}
};
那么Thread.currentThread==这个。如果我们在run方法中,这不是意味着Thread.currentThread==这个吗
否。在Runnable的run方法中,这并不等于当前线程,它表示当前Runnable实例
Runnable只是一个普通对象,可用于构造新线程。使用Runnable构造线程时:
线程和runnable仍然是不同的对象
此exmaple表明它们是不同的东西:
public static void main (String[] args) throws Throwable {
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("inner runnable: " + this); // inner runnable: com.Test$1@34ce8af7
}
};
Thread thread = new Thread(runnable);
System.out.println("thread: " + thread); // thread: Thread[Thread-0,5,main]
System.out.println("runnable: " + runnable); // runnable: com.Test$1@34ce8af7
thread.start();
thread.join();
}
另一件值得注意的事情是java.lang.Thread还实现了Runnable接口,因此如果直接创建线程并重写run方法,则该关键字和Thread.currentThread都将引用当前线程实例
Thread thread = new Thread() {
@Override
public void run() {
// this == Thread.currentThread()
}
};
也可以在任何方法中引用当前线程,无论是否可以在run方法中引用,例如,在main方法中:
如果我们在run方法中,这不是意味着Thread.currentThread==this吗
否。在Runnable的run方法中,这并不等于当前线程,它表示当前Runnable实例
Runnable只是一个普通对象,可用于构造新线程。使用Runnable构造线程时:
线程和runnable仍然是不同的对象
此exmaple表明它们是不同的东西:
public static void main (String[] args) throws Throwable {
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("inner runnable: " + this); // inner runnable: com.Test$1@34ce8af7
}
};
Thread thread = new Thread(runnable);
System.out.println("thread: " + thread); // thread: Thread[Thread-0,5,main]
System.out.println("runnable: " + runnable); // runnable: com.Test$1@34ce8af7
thread.start();
thread.join();
}
另一件值得注意的事情是java.lang.Thread还实现了Runnable接口,因此如果直接创建线程并重写run方法,则该关键字和Thread.currentThread都将引用当前线程实例
Thread thread = new Thread() {
@Override
public void run() {
// this == Thread.currentThread()
}
};
也可以在任何方法中引用当前线程,无论是否可以在run方法中引用,例如,在main方法中:
如果这是线程,则只能将其替换为Thread.currentThread。这将需要扩展线程,这很少是一个好主意
无论如何,您都不需要使用Thread.currentThread这样的异常可以中断循环
try {
while (true){
try {
//foo();
Thread.sleep(1000);
} finally {
//foo();
}
}
} catch (InterruptingException e){
// if the thread needs to continue after an interrupt.
Thread.currentThread().interrupt();
}
我倾向于假设一个被中断的线程应该尽快停止,在这种情况下,被中断后理想情况下不应该有任何工作。如果这是线程,您只能用thread.currentThread替换它。这将需要扩展线程,这很少是一个好主意
无论如何,您都不需要使用Thread.currentThread这样的异常可以中断循环
try {
while (true){
try {
//foo();
Thread.sleep(1000);
} finally {
//foo();
}
}
} catch (InterruptingException e){
// if the thread needs to continue after an interrupt.
Thread.currentThread().interrupt();
}
我倾向于假设一个被中断的线程应该尽快停止,在这种情况下,被中断后理想情况下不应该有任何工作。没有,但感谢您的支持answer@Amin,当然,那不是你提到的那个。谢谢你的提醒。我认为它们是一样的。值得注意的是,线程被中断状态的一般约定是,任何等待很长时间的方法都应该注意线程是否被中断。如果中断阻止该方法执行它通常会执行的操作,它应该向其调用者抛出InterruptedException;如果它正常完成,它应该重新中断线程,这样中断信号就不会丢失。在您创建的线程中,您可以选择忽略中断信号,但在可能在不同线程上运行的可运行线程中,如果发生中断,您通常不应忽略中断。不,但感谢您的支持answer@Amin,当然,那不是你提到的那个。谢谢你的提醒。我认为它们是一样的。值得注意的是,线程被中断状态的一般约定是,任何等待很长时间的方法都应该注意线程是否被中断。如果中断阻止该方法执行它通常会执行的操作,它应该向其调用者抛出InterruptedException;如果它正常完成,它应该重新中断线程,这样中断信号就不会丢失。在您创建的线程中,您可以选择忽略中断信号,但在可能在不同线程上运行的可运行线程中,如果发生中断,通常不应忽略中断。