/**
* 常规interrupt
*
* @author Allen
* @date 2017年2月21日
*
*/
public class e1 implements Runnable {
public static void main(String[] args) {
e1 nm=new e1();
Thread thread = new Thread(nm);
System.out.println("躁起来修电脑去");
System.out.println("interrupt state = " + thread.isInterrupted());
thread.start();
nm.sleep(500);
thread.interrupt();
System.out.println("别修复了,世界末日了");
System.out.println("interrupt state = " + thread.isInterrupted());
}
public void run() {
System.out.println("开始修复电脑");
for (int i = 10; i <= 100; i += 10) {
if(Thread.interrupted())
System.out.println("~~别烦我我得修完了");
System.out.println("修复进度" + i + "%");
sleep(200);
}
System.out.println("修复完毕");
}
/**
* 自己写个sleep条件循环为了禁止Interrupt对Thread.sleep(x)时的异常抛出
* @param step
* @author Allen
* @date 2017年2月21日
*/
private void sleep(int step) {
long time = System.currentTimeMillis();
while ((System.currentTimeMillis() - time < step)) {
}
}
}
/**
* interrupt try/catch销毁版
*
* @author Allen
* @date 2017年2月21日
*
*/
public class e2 implements Runnable {
public static void main(String[] args) throws Exception {
e2 nm = new e2();
Thread thread = new Thread(nm);
System.out.println("躁起来修电脑去");
System.out.println("interrupt state = " + thread.isInterrupted());
thread.start();
Thread.sleep(500);
thread.interrupt();
System.out.println("别修复了,世界末日了");
/**
* 因为Thread.sleep 触发了中断状态 所以interrupted状态被清除
* 所以这里打印的也是false
*/
System.out.println("interrupt state = " + thread.isInterrupted());
}
public void run() {
System.out.println("开始修复电脑");
for (int i = 10; i <= 100; i += 10) {
if (Thread.interrupted())
System.out.println("~~别烦我我得修完了");
System.out.println("修复进度" + i + "%");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("抛异常了被try/catch死神捕获了,只能return了");
return;
}
}
System.out.println("修复完毕");
}
}
躁起来修电脑去
interrupt state = false
开始修复电脑
修复进度10%
修复进度20%
修复进度30%
别修复了,世界末日了
interrupt state = false //这里为什么返回false了呢?
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at TwoPhaseTerminationPattern.interrupt.e2.run(e2.java:34)
at java.lang.Thread.run(Thread.java:724)
抛异常了被try/catch死神捕获了,只能return了
/**
* interrupt i/o阻塞
*
* @author Allen
* @date 2017年2月21日
*
*/
public class e3 implements Runnable {
public static void main(String[] args) throws Exception {
e3 nm = new e3();
Thread thread = new Thread(nm);
System.out.println("燥起来修电脑去");
System.out.println("interrupt state = " + thread.isInterrupted());
thread.start();
Thread.sleep(800);
thread.interrupt();
System.out.println("别修复了,世界末日了");
System.out.println("interrupt state = " + thread.isInterrupted());
}
public void run() {
System.out.println("开始修复电脑");
for (int i = 10; i <= 100; i += 10) {
if (Thread.interrupted())
System.out.println("~~别烦我我得修完了");
System.out.println("修复进度" + i + "%");
if (i == 70) {
try {
System.out.println("建立一个nio ServerSocketChannel我就继续修复");
ServerSocketChannel ss = ServerSocketChannel.open();
ss.socket().bind(new InetSocketAddress(4488));
System.out.println("建立好了");
while (true) {
// 阻塞一下
ss.accept();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("抛异常了被try/catch死神补货了,只能return了");
return;
}
}
}
System.out.println("修复完毕");
}
}
燥起来修电脑去
interrupt state = false
开始修复电脑
修复进度10%
修复进度20%
修复进度30%
修复进度40%
修复进度50%
修复进度60%
修复进度70%
建立一个nio ServerSocketChannel我就继续修复
建立好了
别修复了,世界末日了
interrupt state = true //这里怎么又出现true了,捕获了exception不是应该重置状态么?
java.nio.channels.ClosedByInterruptException
at java.nio.channels.spi.AbstractInterruptibleChannel.end(AbstractInterruptibleChannel.java:202)
at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:248)
at TwoPhaseTerminationPattern.interrupt.e3.run(e3.java:41)
at java.lang.Thread.run(Thread.java:724)
抛异常了被try/catch死神补货了,只能return了
public void interrupt()
Interrupts this thread.
Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.
If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException.
If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.
If none of the previous conditions hold then this thread's interrupt status will be set.
Interrupting a thread that is not alive need not have any effect.
Throws:
SecurityException - if the current thread cannot modify this thread