首页 > 编程语言 > 详细

java 线程终止2种方式及thread.interrupt() Thread.interrupted()方法的使用

时间:2020-05-10 11:54:17      阅读:54      评论:0      收藏:0      [点我收藏+]
/*
 * 文件名:TestInterrupt.java
 * 版权:Copyright 2017-2018 liutao. Co. Ltd. All Rights Reserved.
 * 修改人:Administrator
 * 修改时间:2020年5月10日
 * 修改内容:新建
 */
package test20200510;

import org.junit.Test;

/**
 * 〈一句话功能简述〉 〈功能详细描述〉
 * 
 * @author Administrator
 * @version 1.0 2020年5月10日
 * @see TestInterrupt
 * @since 1.0
 */
public class TestInterrupt {

    public static void main(String[] args) throws InterruptedException {
        // testInterrupted();
        testInterrupted4();
    }

    /**
     * thread.interrupt不能终止正在运行的线程 <一句话功能简述> <功能详细描述> 当时会改变线程的状态值false->true
     * 
     * @throws InterruptedException
     * @see
     * @since 1.0
     */
    @Test
    public void testInterupt() throws InterruptedException {
        Thread thread1 = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                for (int i = 0; i < 1000000; i++) {
                    System.out.println("i:" + i);
                }
            }
        });
        thread1.start();
        Thread.sleep(10);
        thread1.interrupt();
    }

    /***
     * Thread.interrupted 判断当前线程是否终止 并且会清除当前线程的状态值true->false <一句话功能简述> <功能详细描述>
     * 
     * @throws InterruptedException
     * @see
     * @since 1.0
     */
    public static void testInterrupted() throws InterruptedException {
        Thread thread1 = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                for (int i = 0; i < 10000; i++) {
                    System.out.println("i:" + i);
                }
                System.out.println("是否停止 " + Thread.interrupted());
                System.out.println("是否停止 " + Thread.interrupted());
            }
        });
        thread1.start();
        Thread.sleep(10);
        thread1.interrupt();
        // System.out.println("是否停止 " + Thread.interrupted());
        // System.out.println("是否停止 " + Thread.interrupted());
    }

    /**
     * 使用Thread.interruptted+break无法使当前线程立即退出运行 如何解决?使用异常+try catch 又称异常法终止线程
     * 
     * 线程状态值默认是false 如果调用interrupt,则变为true 使用Thread.interrupted()则会将true->false
     * <一句话功能简述> <功能详细描述>
     * 
     * @throws InterruptedException
     * @see
     * @since 1.0
     */
    public static void testInterrupted2() throws InterruptedException {
        Thread thread1 = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    for (int i = 0; i < 10000; i++) {
System.out.println(
"i:" + i + Thread.currentThread().isInterrupted()); if (Thread.interrupted()) { System.out.println("线程终止。。。 退出。。。" + Thread.currentThread().isInterrupted()); throw new InterruptedException(); } } System.out.println("end-------"); } catch (Exception e) { // TODO: handle exception System.out.println("Mythread ex:" + e); } // TODO Auto-generated method stub } }); thread1.start(); Thread.sleep(10); thread1.interrupt(); } /** * 如果当前线程正则sleep 这时去interrupt 则会直接抛出InterruptedException 并且将状态值改为false * <一句话功能简述> <功能详细描述> * * @throws InterruptedException * @see * @since 1.0 */ public static void testInterrupted3() throws InterruptedException { Thread thread1 = new Thread(new Runnable() { @Override public void run() { try { for (int i = 0; i < 10000; i++) { System.out.println("i:" + i); } Thread.sleep(10000); System.out.println("end-------"); } catch (Exception e) { System.out.println("当前线程状态:" + Thread.interrupted()); System.out.println("Mythread ex:" + e); } } }); thread1.start(); Thread.sleep(10); thread1.interrupt(); } volatile static boolean flag = true; /** * 终止线程推荐做法 标志位 <一句话功能简述> <功能详细描述> * * @throws InterruptedException * @see * @since 1.0 */ public static void testInterrupted4() throws InterruptedException { Thread thread1 = new Thread(new Runnable() { @Override public void run() { int i = 0; while (flag) { System.out.println(i++); } } }); thread1.start(); Thread.sleep(100); flag = false; } }


 

为什么现在不使用 thread.stop来终止线程? (在线程run方法中Thread.currentThread()==this)

1 此方法已废弃

2 此方法过于暴力,如果当前线程在操作外部资源,调用stop方法,会导致catch finally中代码不执行,资源不释放

3 数据不一致

java 线程终止2种方式及thread.interrupt() Thread.interrupted()方法的使用

原文:https://www.cnblogs.com/lt123/p/12862574.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!