Java多线程(五)停止线程 在沉睡中停止线程

在沉睡中停止线程会抛出异常

public class SleepInterruptDemo extends Thread {

    public void run() {
        super.run();
        try {

            for (int i = 0; i < 500000; i++) {
                System.out.println("i=" + (i + 1));
            }
            System.out.println("run begin");
            Thread.sleep(2000);
            System.out.println("run end");
        } catch (Exception e) {
            System.out.println("进入run方法中的catch了!!");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        SleepInterruptDemo thread = new SleepInterruptDemo();
        thread.start();
        thread.interrupt();
        System.out.println("main end!");
    }
}

 

posted @ 2017-09-26 10:11  newlangwen  阅读(591)  评论(0编辑  收藏  举报