java线程中断之interrupt和stop

java线程中断之interrupt和stop

interrupt方法

当我们调用线程的sleep方法或者join方法时,都可以让某一些线程处于等待状态,调用当前线程的interrupt()方法就可以打断该阻塞状态。interrupt方法不会让线程结束。

1publicvoid interrupt();// 中断线程的阻塞状态

此方法会抛出一个InterruptedException异常。

java相关学习视频推荐:java课程

案例:演示中断sleep的等待状态

线程类:

package com.pbteach.thread;

publicclassMyThread extendsThread {

       @Override

       publicvoid run() {

           for(int x = 0 ; x < 100 ; x++) {

               System.out.println(Thread.currentThread().getName() + "----"+ x );

               if(x == 10) {

                   try{

                       TimeUnit.SECONDS.sleep(10);     // 线程休眠以后,该线程就处于阻塞状态

                   } catch(InterruptedException e) {

                       e.printStackTrace();

                   }

               }

           }

       }

   }

测试类:

package com.pbteach.thread;

publicclassThreadDemo {

    publicstaticvoid main(String[] args) {

        // 创建MyThread线程对象

        MyThread t1 = newMyThread();

        t1.setName("pbteach-01");

        // 启动线程

        t1.start();

        try{

            // 主线程休眠2秒

            TimeUnit.SECONDS.sleep(2);

        } catch(InterruptedException e) {

            e.printStackTrace();

        }

        // 中断t1线程的休眠

        t1.interrupt();

    }

}

输出结果:

...

pbteach-01----10

java.lang.InterruptedException: sleep interrupted

    at java.base/java.lang.Thread.sleep(Native Method)

    at java.base/java.lang.Thread.sleep(Thread.java:339)

    at java.base/java.util.concurrent.TimeUnit.sleep(TimeUnit.java:446)

    at com.pbteach.javase.thread.api.demo14.MyThread.run(MyThread.java:14)

pbteach-01----11

...

通过控制台的输出结果,我们可以看到interrupted方法并没有去结束当前线程,而是将线程的阻塞状态中断了,中断阻塞状态以后,线程pbteach-01继续进行执行。

stop方法

调用线程的stop方法可以让线程终止执行。

1publicfinalvoid stop()  // 终止线程的执行

线程类

package com.pbteach.thread;

    publicclassMyThread extendsThread {

        @Override

        publicvoid run() {

            for(int x = 0 ; x < 100 ; x++) {

                System.out.println(Thread.currentThread().getName() + "----"+ x );

                if(x == 10) {

                    try{

                        TimeUnit.SECONDS.sleep(10);     // 线程休眠以后,该线程就处于阻塞状态

                    } catch(InterruptedException e) {

                        e.printStackTrace();

                    }

                }

            }

        }

    }

测试类:

package com.pbteach.thread;

    publicclassThreadDemo1 {

        public static void main(String[] args) {

            // 创建MyThread线程对象

            MyThread t1 = newMyThread();

            t1.setName("pbteach-01");

            // 启动线程

            t1.start();

            try{

                // 主线程休眠2秒

                TimeUnit.SECONDS.sleep(2);

            } catch(InterruptedException e) {

                e.printStackTrace();

            }

            // 终止线程t1的执行

            t1.stop();

        }

    }

输出结果:

...

pbteach-01----9

pbteach-01----10

控制台没有任何异常的的输出,程序结束,"pbteach-01"线程没有继续进行执行。

相关文章教程推荐:java编程入门

你可能感兴趣的:(java线程中断之interrupt和stop)