java的thread state、daemon、priority

这篇博客介绍了如何在Java中查看线程状态,设置线程优先级以及创建守护线程。通过示例代码展示了线程的启动、状态检测,以及如何使用`setPriority()`和`setDaemon(true)`方法来调整线程的执行顺序和生命周期。
摘要由CSDN通过智能技术生成

查看线程的状态

package com.seven.threadState;

public class StateForThread {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("------------");
        });

        //检测线程的状态
        Thread.State state = thread.getState();
        System.out.println(state);

        thread.start();
        while (true){
            //只要线程没有终止,就可以一直获取状态
            Thread.sleep(500);
            state = thread.getState();
            System.out.println(state);
            if (state==Thread.State.TERMINATED) break;
        }


    }
}

设置线程的优先级

package com.seven.threadState;

/**
 * 设置线程的优先级:
 * 一定要先设置,再启动,
 * 级别从0-10,代表从低到高,优先级越高,被cpu选中的概率高,而不是绝对的
 * thread.setPriority(10);
 */
public class Priority {
    public static void main(String[] args) {
        MyThreadPriority myThreadPriority = new MyThreadPriority();

        Thread thread1 = new Thread(myThreadPriority,"1");
        Thread thread2 = new Thread(myThreadPriority,"2");
        Thread thread3 = new Thread(myThreadPriority,"3");
        Thread thread4 = new Thread(myThreadPriority,"4");
        thread3.setDaemon(true);


        thread1.setPriority(6);
        thread1.start();

        thread2.setPriority(8);
        thread2.start();

        thread3.setPriority(2);
        thread3.start();

        thread4.start();


    }
}
class MyThreadPriority implements Runnable
{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"优先级是---"+Thread.currentThread().getPriority());
    }
}

设置守护线程

package com.seven.threadState;

/**
 * 线程分为用户线程、守护线程
 * 守护线程:jvm必须保证用户线程执行完毕,但不用等待守护线程执行完毕,
 * 用户线程执行完(守护线程自动结束)
 */
public class Daemon {
    public static void main(String[] args) {

        DaemonThread daemonThread = new DaemonThread();
        UserThread userThread = new UserThread();

        Thread thread = new Thread(daemonThread);
        Thread thread1 = new Thread(userThread);

        thread.setDaemon(true);//设置守护线程为true
        thread.start();

        thread1.start();//用户线程结束,守护线程自动结束
    }
}
class DaemonThread implements Runnable{

    @Override
    public void run() {
        int i=0;
        while (true){
            System.out.println("daemonThread活着"+i++);
        }
    }
}

class UserThread implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 36000; i++) {
            System.out.println("我活着"+i);
        }
        System.out.println("我死了");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值