4.线程属性

1.属性概述

属性名称用途描述
编号(Id)用于表示不同的线程,该属性从1开始递增并且不可更改
名称(Name)用于区分线程,便于开发者在开发过程中定位问题
是否是守护线程(isDaemon)true代表该线程是守护线程,false代表线程是非守护线程(用户线程)
优先级(Priority)用于调度线程,告诉线程调度器哪些线程相对多运行、哪些相对少运行

2.线程编号

public class ThreadId {
    public static void main(String[] args) {
        System.out.println("主线程的Id:" + Thread.currentThread().getId());
        Thread thread = new Thread();
        System.out.println("子线程的Id:" + thread.getId());
    }
}

代码运行结果如下所示,子线程Id不为2的原因是,JVM在运行该程序时,除了创建main线程外,还创建了包括Finalizer、Reference Handler、Signal Dispatcher等线程。

主线程的Id:1
子线程的Id:9

3.线程名称

public class Thread implements Runnable {
	//线程默认名称
	public Thread() {
	    init(null, null, "Thread-" + nextThreadNum(), 0);
	}
	
 	private static int threadInitNumber;
 	
    private static synchronized int nextThreadNum() {
        return threadInitNumber++;
    }
    
    //修改线程名称
	private volatile String name;

	public final synchronized void setName(String name) {
        checkAccess();
        if (name == null) {
            throw new NullPointerException("name cannot be null");
        }

        this.name = name;
        if (threadStatus != 0) {
            setNativeName(name);
        }
    }

	private native void setNativeName(String name);
}

由上述源码可知,线程默认名称是以“Thread-”开头加一个自增的threadInitNumber变量组成。线程在创建时,如果没有设置名称,可以通过setName来修改线程名称。

4.守护线程
守护线程的作用是给用户线程提供服务的,比如垃圾回收线程。当JVM发现用户线程全部运行结束时,JVM便会退出。
(1).守护线程和普通线程的区别?
答:守护线程和普通线程的核心区别是能否决定JVM的退出,普通线程会影响JVM的退出而守护线程不会。

(2).是否需要将线程设置为守护线程?
答:不应该将线程设置为守护线程,原因是当程序中只剩下守护线程时,JVM便会退出,这将会导致线程任务没有执行完毕。

5.线程的优先级
程序中的多线程是并发执行的,在某个线程想要被执行就必须要获得CPU的执行权。JVM为线程分配CPU执行权的机制被称为线程的调度。线程调度主要有两种模型,分时调度模型和抢占式调度模型。分时调度模型是指平均分配CPU时间片段,让线程轮流执行。抢占式调度模型是指让线程优先级高的先运行,优先级相同的随机选择一个运行,这是Java默认采用的调度模型。
Java共有10个线程优先级等级,用1-10来表示,10最高,可通过setPriority()来设置优先级,参数为1-10或者3个常量,具体源码和示例如下所示。

public class Thread implements Runnable {
 	public final static int MIN_PRIORITY = 1;
 	
    public final static int NORM_PRIORITY = 5;
    
    public final static int MAX_PRIORITY = 10;
    
	public final void setPriority(int newPriority) {
        ThreadGroup g;
        checkAccess();
        if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
            throw new IllegalArgumentException();
        }
        if((g = getThreadGroup()) != null) {
            if (newPriority > g.getMaxPriority()) {
                newPriority = g.getMaxPriority();
            }
            setPriority0(priority = newPriority);
        }
    }
    
	private native void setPriority0(int newPriority);
}
/**
 * 描述:证明设置线程的优先级会影响线程执行顺序。
 */
public class ThreadPriority implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + "...正在输出..." + i);
        }
    }

    public static void main(String[] args) {
        ThreadPriority threadPriority = new ThreadPriority();
        Thread thread0 = new Thread(threadPriority);
        Thread thread1 = new Thread(threadPriority);
        thread0.setPriority(1);
        thread1.setPriority(10);
        thread0.start();
        thread1.start();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值