- public classMyThreadextends Thread {
- @Override
- public void run() {
- super.run();
- try {
- for(inti = 0; i < 5000000; i++) {
- if(this.interrupted()){
- System.out.println("我要停止了。。。。。");
- throw new InterruptedException(); \\抛出异常
- }
- System.out.println("i="+(i+1));
- }
- System.out.println("我在for下边。。");
- } catch (InterruptedException e) {
- System.out.println(" in MyThread catch..");
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- try {
- MyThread myThread=new MyThread();
- myThread.start();
- Thread.sleep(2000);
- myThread.interrupt();
- } catch (InterruptedException e) {
- System.out.println("main catch");
- e.printStackTrace();
- }
- System.out.println("end");
- }
- }
来源: http://www.bubuko.com/infodetail-1972947.html