Java拾贝第八天——线程的状态和常用方法

Java拾贝不建议作为0基础学习,都是本人想到什么写什么

任何线程一般具有5种状态。(非生命周期)

  1. 创建状态:新建了一个线程对象,但还处于不可运行状态。

  2. 就绪状态:新建线程后,调用该线程start()方法就可以启动线程。此时线程进入线程队列排队,并等待CPU分配资源。当线程启动时,进入该状态。

  3. 运行状态:获得CPU资源,线程进入运行状态。自动调用run()方法。

  4. 堵塞状态:一个正在执行的线程因为某种原因。让出CPU资源并暂时中止自己的执行,进入该状态。堵塞时,线程不能进入排队队列,只有堵塞原因消除,才可以转入就绪状态。

  5. 死亡状态:run()方法执行结束后,进入该状态。进入该状态的线程不再具有继续运行的能力。

获得线程名

public Thread(Runnable target, String name) {}//构造方法,String为线程名

Thread.currentThread().getName();//静态方法,currentThread()返回当前正在执行的线程
public class Test8 {
    public static void main(String[] args) {
        new Thread(new MyThread(),"自定线程A").start();
        new Thread(new MyThread(),"自定线程B").start();
		//打印main方法线程名
        System.out.println(Thread.currentThread().getName());
    }
}
class MyThread implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+" + hello");
    }
}

程序运行结果:

main
自定线程B + hello
自定线程A + hello

线程是否活动

Thread.isAlive();//普通方法 返回布尔值 T处于活动状态否则F
public class Test8 {
    public static void main(String[] args) {
        Thread a = new Thread(new MyThread(), "线程A");
        System.out.println("线程启动前"+a.isAlive());
        a.start();
        System.out.println("线程启动后"+a.isAlive());
        for (int i = 0; i <3 ; i++) {
            System.out.println(Thread.currentThread().getName()+" + "+i);
        }
        System.out.println("for循环之后"+a.isAlive());//每次运行结果不确定
    }
}
class MyThread implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+" + hello");
    }
}

程序运行结果:

线程启动前false
线程启动后true
main + 0
main + 1
main + 2
for循环之后true
线程A + hello

线程插队

Thread.join();//普通方法 强制让该线程运行。
public class Test8 {
    public static void main(String[] args) {
        Thread a = new Thread(new MyThread(), "线程A");
        a.start();
        for (int i = 0; i <20 ; i++) {
            System.out.println(Thread.currentThread().getName()+i);
            if (i==10){
                try {
                    a.join();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

    }
}
class MyThread implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i <20 ; i++) {
            System.out.println(Thread.currentThread().getName()+" + hello"+i);
        }

    }
}

程序运行结果:

main0
线程A + hello0
main1
线程A + hello1
main2
线程A + hello2
main3
线程A + hello3
main4
线程A + hello4
main5
main6
线程A + hello5
线程A + hello6
线程A + hello7
main7
线程A + hello8
main8
main9
线程A + hello9
main10
i=10,此时线程A插队
线程A + hello10
线程A + hello11
线程A + hello12
线程A + hello13
线程A + hello14
线程A + hello15
线程A + hello16
线程A + hello17
线程A + hello18
线程A + hello19
main11
main12
main13
main14
main15
main16
main17
main18
main19

守护线程

Java程序入口就是由JVM启动main线程,main线程又可以启动其他线程。当所有线程都运行结束时,JVM退出,进程结束。JVM退出时,不必关心守护线程是否已结束。

如果某线程需要无限循环执行某定时任务可以将其设置为守护线程:

public class Test8 {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.setDaemon(true);
        thread.start();
    }
}
class MyThread extends Thread{
    @Override
    public void run() {
        while (true){
            System.out.println(LocalTime.now());//获取当前时间  工具类
            try {
                Thread.sleep(60,000);//每1分钟打印一次时间,1000=1秒
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }
}

礼让线程

Thread.yield() //普通方法 礼让线程
public class Test8 {
    public static void main(String[] args) {
        new Thread(new MyThread(), "线程A").start();
        for (int i = 0; i < 30; i++) {
            System.out.println(Thread.currentThread().getName()+i);
            if (i==20){
                Thread.yield();//主线程循环20,就礼让线程
            }
        }

    }
}
class MyThread implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i <20 ; i++) {
            System.out.println(Thread.currentThread().getName()+" + hello"+i);
        }

    }
}

程序运行结果:

main0
线程A + hello0
main1
main2
main3
线程A + hello1
main4
线程A + hello2
main5
线程A + hello3
main6
线程A + hello4
线程A + hello5
线程A + hello6
线程A + hello7
main7
线程A + hello8
main8
main9
main10
main11
线程A + hello9
main12
main13
main14
main15
main16
main17
线程A + hello10
main18
main19
main20
线程A + hello11
线程A + hello12
线程A + hello13
线程A + hello14
线程A + hello15
线程A + hello16
线程A + hello17
线程A + hello18
线程A + hello19
main21
main22
main23
main24
main25
main26
main27
main28
main29
posted @ 2023-10-21 21:50  rowbed  阅读(3)  评论(0编辑  收藏  举报