public class InterruptCheck extends Object {
public static void main(String[] args) {
Thread t = Thread.currentThread();
System.out.println("Point A: t.isInterrupted()=" + t.isInterrupted());
t.interrupt();
System.out.println("Point B: t.isInterrupted()=" + t.isInterrupted());
System.out.println("Point C: t.isInterrupted()=" + t.isInterrupted()); try {
Thread.sleep(2000);
System.out.println("was NOT interrupted");
} catch ( InterruptedException x ) {
System.out.println("was interrupted");
} System.out.println("Point D: t.isInterrupted()=" + t.isInterrupted());
}
}
(1)主线程已经中断了问什么程序还能往下执行???难道只有main线程有此特性??public class SleepInterrupt extends Object implements Runnable {
public static void main(String[] args) {
SleepInterrupt si = new SleepInterrupt();
Thread t = new Thread(si);
t.start(); // Be sure that the new thread gets a chance to
// run for a while.
try { Thread.sleep(2000); }
catch ( InterruptedException x ) { } System.out.println("in main() thread");
// t.interrupt();
System.out.println("t.isInterrupted()"+ t.isInterrupted());
System.out.println("in main() - leaving");
}
public void run() {
System.out.println("in run() - doing things after nap");
for(int i=0;i<100000;i++);
System.out.println("in run() - leaving normally");
}
}
(2)为什么在线程t没有被interrupt的情况下,t线程没输出?
public static void main(String[] args) {
Thread t = Thread.currentThread();
System.out.println("Point A: t.isInterrupted()=" + t.isInterrupted());
t.interrupt();
System.out.println("Point B: t.isInterrupted()=" + t.isInterrupted());
System.out.println("Point C: t.isInterrupted()=" + t.isInterrupted()); try {
Thread.sleep(2000);
System.out.println("was NOT interrupted");
} catch ( InterruptedException x ) {
System.out.println("was interrupted");
} System.out.println("Point D: t.isInterrupted()=" + t.isInterrupted());
}
}
(1)主线程已经中断了问什么程序还能往下执行???难道只有main线程有此特性??public class SleepInterrupt extends Object implements Runnable {
public static void main(String[] args) {
SleepInterrupt si = new SleepInterrupt();
Thread t = new Thread(si);
t.start(); // Be sure that the new thread gets a chance to
// run for a while.
try { Thread.sleep(2000); }
catch ( InterruptedException x ) { } System.out.println("in main() thread");
// t.interrupt();
System.out.println("t.isInterrupted()"+ t.isInterrupted());
System.out.println("in main() - leaving");
}
public void run() {
System.out.println("in run() - doing things after nap");
for(int i=0;i<100000;i++);
System.out.println("in run() - leaving normally");
}
}
(2)为什么在线程t没有被interrupt的情况下,t线程没输出?
解决方案 »
- java颜色转换
- java输入问题
- 请教一个问题:Exception in thread "main" java.lang.NullPointerException
- getImage()问题?
- 有对JNI熟的高人吗?有个问题想破了头也不知道是怎么会事,MD,JNI难道只能调用a*b+c=?这种弱智东东吗?
- mysql支持PreparedStatement吗?遇到问题了。
- 用java杀当前系统线程怎么写?
- 关于 private 修饰符的用法的一点疑问
- 求指导,为什么JavaIO流的flush没用,刷不出去数据?
- 大家好,我是新人,怎用才能入门java?????????
- 如何控制JTextField 中的输入格式,如只能输入数字、最大只能输入n位,多了则不能输入
- 容器怎样响应它自己里面的组件的事件?
如果没有注释掉t.interrupt()那么System.out.println("t.isInterrupted()"+ t.isInterrupted())
还是输出false,问什么???
这是个线程自身中断的问题,当线程调用interrupt()时,有三种情况这个中断会起作用,
1.当它处于wait()或join()中时,这时它会收到一个 InterruptedException. 这种是要try catch的.
2.当它在一个interruptible channel 中阻塞时会收到一一个ClosedByInterruptException.
3.当它在一个selector 里阻塞时.
如果这三种情况都不满足,那么这个线程只是简单的把设置为true,其他什么事都不干,继续运行,等到下面try catch时发现这个线程的中断状态为true,则catch语句块被执行,中断状态又被设置为法false,所以最后的输出又是发false了.
总的来说,其实线程没有被中断,只是中断状态值被设置成了true.(2)
不好意思,第二个问题在我机子上一点问题都没有,输出很正常:in run() - doing things after nap
in run() - leaving normally
in main() thread
t.isInterrupted()false
in main() - leaving
Press any key to continue...
true if the current thread has been interrupted; false otherwise.
See Also:
isInterrupted()
public static void sleep(long millis)
throws InterruptedExceptionCauses the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors. Parameters:
millis - the length of time to sleep in milliseconds.
Throws:
InterruptedException - if another thread has interrupted the current thread. "The interrupted status of the current thread is cleared when this exception is thrown."
See Also:
Object.notify()
public static void main(String[] args) {
// 取得main线程
Thread t = Thread.currentThread();
// false
System.out.println("Point A: t.isInterrupted()=" + t.isInterrupted());
// 仅设置该中断状态为true,见:api doc: public void interrupt()
t.interrupt();
// true
System.out.println("Point B: t.isInterrupted()=" + t.isInterrupted());
// true
System.out.println("Point C: t.isInterrupted()=" + t.isInterrupted());try {
Thread.sleep(2000);
System.out.println("was NOT interrupted");
} catch ( InterruptedException x ) {
// The interrupted status of the current thread is cleared when this exception is thrown.
System.out.println("was interrupted");
}
// false
System.out.println("Point D: t.isInterrupted()=" + t.isInterrupted());
}
}