java Thread 线程的中断 interrupt

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
 * java  Thread 协作式 interrupt 测试例子
 * @author geliang
 *
 */
public class Main {
	public static void main(String[] args) throws InterruptedException {
		MyThread t;
		t = new MyThread();
		t.start();
		try {
			Thread.sleep(1);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 t.interrupt();
		System.out.println("已调用线程的interrupt方法" + t.isInterrupted());
		
	}
}
class MyThread extends Thread {
	public Object lock = new Object(); ;
	public void run() {
			int num = 0;
				try {
					num = longTimeRunningNonInterruptMethod(2, 0);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			System.out.println("长时间任务运行结束,num=" + num);
			System.out.println("线程运行结束 线程的中断状态:" + Thread.interrupted()+"如果为true 说明线程并未被打断");
		
	}

	private  synchronized  int longTimeRunningNonInterruptMethod(int count,
			int initNum) throws InterruptedException, IOException {
//		if (interrupted()) {
//			throw new InterruptedException("正式处理前线程已经被请求中断");
//		}

			long startTime = System.currentTimeMillis();
			Thread.sleep(0);//在这里设定可以在IO前 中断线程
			byte[] bytes=new byte[19*1024*1024];
			System.out.println("开始IO");
			FileOutputStream fliFileOutputStream=new FileOutputStream(new File("e:\\temp"));
			fliFileOutputStream.write(bytes);
			fliFileOutputStream.flush();
			fliFileOutputStream.close();
			System.out.println("endIO");//无法打断IO 。因为IO的代码中没有 sleep wait之类的方法。
			System.out.println("IO TIME:"+(System.currentTimeMillis()-startTime));
			
			System.out.println("开始耗时计算");
			startTime = System.currentTimeMillis();
			Thread.sleep(0);//设定可以再计算过程前 中断线程
			for (int i = 0; i < count; i++) {
				
				for (int j = 0; j < Integer.MAX_VALUE; j++) {
					Thread.sleep(0);//设定可以再计算过程中 中断线程
					initNum++;
				}
			}
			System.out.println("结束计算");
			System.out.println("计算 TIME:"+(System.currentTimeMillis()-startTime));
			
			// 下面两行代码阐述了interrupt 的本质。 调用sleep wait join 才会抛出 异常的原因

			if (interrupted()) {
				// 回滚数据,清理操作等
				throw new InterruptedException("线程正在处理过程中被中断");
			}

		return initNum;
	}
}
/**
 * 结论:所谓协作式中断 就是 thread 类有个boolean ,调用 interrupt()方法 会将这个变量设置为true;</br>
 * 如果线程设置了 sleep wait ,JVM会在thread时间片切换的间隙 轮询这个boolean,如果为true 就会尽快的抛出一个异常 并在底层取消掉这个线程</br>
 * 不能打断一个 被堵塞的(或者叫正在长时间运行的)线程 比如 io 读写,socket 收发。()
 * 最后 我产生的疑问 :java 如何强行终止一个线程?(阻止掉一个被一些操作线程阻塞,无法轮询 那个boolean的线程)
 *其实这个问题,就是java 协作式 终止的本质----> 发明出java语言的人 把这个难题抛给了使用JAVA编程的人-->>你自己去解决吧!! 
 *靠! 

各位看官如有不同意见或者发现错误 请联系 qq:254017183 共同学习 (NIO 可以解决IO读写的堵塞问题)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值