栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

线程的结束方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

线程的结束方式

1、自然结束,线程能够顺利完成执行(能自然结束就自然结束)
2、废弃方法stop()、suspend()、resume(),容易造成线程不一致或者死锁的问题

    @Test
    public void function(){
        Thread t = new Thread(() -> {
            while (true){
                try {
                    TimeUnit.SECONDS.sleep(2);
                    System.out.println("t is running");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();

        try {
            TimeUnit.SECONDS.sleep(6);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //stop()方法会释放掉当前线程持有的锁,从而造成数据不一致的问题
        t.stop();
    }
package com.yang.Threads;

import java.util.concurrent.TimeUnit;


public class TestMain {
    public static void main(String[] args) {
        Thread t2 = new Thread(()->{
            while(true){
                try {
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println("t2 is running");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t2.start();

        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.suspend();
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.resume();
    }
}

3、使用volatile定义的标识位(可取)

package com.yang.Threads;

import java.util.concurrent.TimeUnit;


public class TestMain1 {

    private static volatile boolean running = true;

    public static void main(String[] args) {
        Thread t = new Thread(()->{
            long i = 0L;
            while (running){
                //如果里边包含wait()等方法,循环内部会阻塞住,无法结束
                i++;
            }
            System.out.println("最终结果result为:"+i);
        });
        t.start();
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            running = false;
        }
    }
}

4、使用interrupt()给线程设置标识位(可取,相对优雅)

package com.yang.Threads;

import java.util.concurrent.TimeUnit;


public class TestMain2 {


    public static void main(String[] args) {
        Thread t = new Thread(()->{
            long i = 0L;
            //可以使用!Thread.interrupted()或者!Thread.currentThread().isInterrupted()判断标识位
            while (!Thread.currentThread().isInterrupted()){
                i++;
            }
            System.out.println("最终结果result为:"+i);
        });
        t.start();
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            //给t设置标识位
            t.interrupt();
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1006009.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号