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

Thread类的属性和方法

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

Thread类的属性和方法

1. Thread(String name) 给线程起名字~

//给线程起名字
public class Demo03 {
    public static void main(String[] args) {
        Thread t = new Thread(()->{
            System.out.println("子线程的名字是:"+Thread.currentThread().getName());
        },"Thread线程");
        t.start();
    }
}
子线程的名字是:Thread线程
Process finished with exit code 0

2. getId() 线程的唯一标识,不同线程不会重复~

//线程ID
public class Demo04 {
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"的ID是:"+Thread.currentThread().getId());
        },"线程1");
        t1.start();

        Thread t2 = new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"的ID是:"+Thread.currentThread().getId());
        },"线程2");
        t2.start();
    }
}
线程1的ID是:16
线程2的ID是:17
Process finished with exit code 0

3. isDaemon() 是否是后台线程~

        后台线程指在程序运行的时候在后台提供一种通用服务的线程,并且这种线程并不属于程序中不可或缺的部分~

        JVM会在一个进程的所有非后台线程结束后,才会结束运行~

//isDaemon 是否是后台线程
public class Demo05 {
    public static void main(String[] args) {
        Thread t = new Thread(()->{
            System.out.println(Thread.currentThread().getName()+" "+Thread.currentThread().isDaemon()+" 后台线程");
        });
        t.start();
    }
}
Thread-0 false 后台线程
Process finished with exit code 0

4. isAlive() 是否存活

可以简单理解为 run()方法是否运行结束

//isAlive  是否存活,run方法是否运行结束
public class Demo06 {
    public static void main(String[] args) {
        Thread t = new Thread(()->{
            System.out.println(Thread.currentThread().getName()+" "+Thread.currentThread().isAlive());
        });
        t.start();
    }
}
Thread-0 true
Process finished with exit code 0

5. 中断线程

        5.1 自定义标志位来中断线程

//使用标志位来中断线程
public class Demo07 {
    private static boolean isQuit = false;
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            while(!isQuit){
                System.out.println("运行子线程");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
        Thread.sleep(5000);
        isQuit = true;
        System.out.println("终止子线程");
    }
}
运行子线程
运行子线程
运行子线程
运行子线程
运行子线程
终止子线程
Process finished with exit code 0

        5.2 使用Thread.currentThread().isInterrupted() 来定义标志位

//使用Thread.currentThread().isInterrupted() 来定义标志位
public class Demo08 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            while(!Thread.currentThread().isInterrupted()){
                System.out.println("运行子线程");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;//当触发异常后,立即退出循环
                }
            }
        });
        t.start();
        Thread.sleep(5000);
        t.interrupt();//t线程被中断
                      //如果t线程处于就绪状态,则线程被中断
                      //如果线程处于阻塞状态,则会抛出异常
    }
}
运行子线程
运行子线程
运行子线程
运行子线程
运行子线程
java.lang.InterruptedException: sleep interrupted
    at java.base/java.lang.Thread.sleep(Native Method)
    at Demo0616.Demo08.lambda$main$0(Demo08.java:9)
    at java.base/java.lang.Thread.run(Thread.java:831)
Process finished with exit code 0

6. join()线程等待--插队

//线程等待(插队)
public class Demo09 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            for (int i = 0; i < 5; i++) {
                System.out.println("子线程");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
        t.join(10000)//让t在10秒内先执行
        for (int i = 0; i < 5; i++) {
            System.out.println("主线程");
            Thread.sleep(1000);
        }
    }
}
子线程
子线程
子线程
子线程
子线程
主线程
主线程
主线程
主线程
主线程
Process finished with exit code 0
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/981650.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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