线程优先级
线程的优先级用数字表示,范围从1~10.
Thread.MIN PRIORITY = 1;
Thread.MAX PRIORITY = 10;
Thread.NORM PRIORITY = 5;
使用以下方式改变或获取优先级
getPriority() . setPriority(int xxx)
//先设置优先级,再启动
t4. setPr iority (Thread . MAX_ PRIORITY); //MAX_ PRIORITY=10
t4.start() ;
优先级低只是意味着获得调度的概率低,并不是优先级低就不会被调用了,还是要看CPU的调度
守护线程
Thread.setDaemon();
线程分为用户线程和守护线程; 虚拟机必须确保用户线程执行完毕
虚拟机不用等待守护线程执行完毕,如:后台记录操作日志,监控内存,垃圾回收等待..
public static void main(String[] args) {
God god = new God() ;
You you = new You() ;
Thread thread = new Thread(god) ;
thread . setDaemon(true); //默认 是false表示是用户线程,正常的线程都是用户线程...
thread. start(); //上帝守护线程启动
new Thread(you) .start(); //你 用户线程启动...
}