1.8 线程的优先级
在操作系统中,线程可以划分优先级,优先级较高的线程得到的cpu的资源较多,也就是cpu优先执行优先级较高的线程对象中的任务.
在Java中,线程优先级分为1~10个等级,如果小于1大于10,则JDK抛出异常,源码如下:
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);
}
}
测试用例:
public class PriorityThread1 extends Thread{
@Override
public void run() {
System.out.println("PriorityThread1 run priority=" + this.getPriority());
PriorityThread2 thread2 = new PriorityThread2();
thread2.start();
}
public static void main(String[] args) {
System.out.println("main thread begin priority = " + Thread.currentThread().getPriority());
// Thread.currentThread().setPriority(6);
System.out.println("main thread end priority =" + Thread.currentThread().getPriority());
new PriorityThread1().start();
}
}
public class PriorityThread2 extends Thread{
@Override
public void run() {
System.out.println("PriorityThread2 run priority=" + this.getPriority());
}
}
打印结果如下(优先级被继承):
main thread begin priority = 5
main thread end priority =5
PriorityThread1 run priority=5
PriorityThread2 run priority=5
去掉代码中的注释打印结果如下(优先级被修改继续被继承):
main thread begin priority = 5
main thread end priority =6
PriorityThread1 run priority=6
PriorityThread2 run priority=6
1.10.2 优先级具有规则性
虽然可以设置优先级,但是没有看到优先级带来的效果
public class PriorityThread1 extends Thread{
@Override
public void run() {
long begin = System.currentTimeMillis();
long addResult = 0;
for(int i=0; i<10; i++){
for(int j=0; j<50000; j++){
Random random = new Random();
random.nextInt();
addResult = addResult + i;
}
}
long end = System.currentTimeMillis();
System.out.println("❤ ❤ ❤ ❤ ❤ thread1 use time " +(end - begin)+ " millisecond");
}
public static void main(String[] args) {
for (int i=0; i<5; i++) {
PriorityThread1 thread1 = new PriorityThread1();
thread1.setPriority(10);
thread1.start();
PriorityThread2 thread2 = new PriorityThread2();
thread2.setPriority(1);
thread2.start();
}
}
}
public class PriorityThread2 extends Thread{
@Override
public void run() {
long begin = System.currentTimeMillis();
long addResult = 0;
for(int i=0; i<10; i++){
for(int j=0; j<50000; j++){
Random random = new Random();
random.nextInt();
addResult = addResult + i;
}
}
long end = System.currentTimeMillis();
System.out.println("☆ ☆ ☆ ☆ ☆ thread2 use time " +(end - begin)+ " millisecond");
}
}
打印结果如下:
☆ ☆ ☆ ☆ ☆ thread2 use time 217 millisecond
☆ ☆ ☆ ☆ ☆ thread2 use time 291 millisecond
❤ ❤ ❤ ❤ ❤ thread1 use time 302 millisecond
❤ ❤ ❤ ❤ ❤ thread1 use time 295 millisecond
❤ ❤ ❤ ❤ ❤ thread1 use time 320 millisecond
❤ ❤ ❤ ❤ ❤ thread1 use time 272 millisecond
❤ ❤ ❤ ❤ ❤ thread1 use time 338 millisecond
☆ ☆ ☆ ☆ ☆ thread2 use time 316 millisecond
☆ ☆ ☆ ☆ ☆ thread2 use time 286 millisecond
☆ ☆ ☆ ☆ ☆ thread2 use time 341 millisecond
由结果可以得知,高优先级的线程总是大部分先执行完,但不代表高优先级的线程全部执行完(也有可能是高优先级的最后才执行完,但是大部分都是先执行的).另外不要以为高优先级的线程被main线程调用就会先执行完,出现这样的结果全是因为其优先级是最高值10造成的.但线程优先级的等级差距很大时,谁先执行完和代码的调用顺序无关,cpu尽量将执行资源让给优先级高的线程.
1.10.3 优先级具有随机性
前面案例介绍了线程的优先级较高则优先执行完run()方法中的任务,但这个结果不能说的太肯定,因为线程的优先级还具有随机性,也就是优先级较高的线程不一定每一次都先执行完
1.11 守护线程
在Java线程中有两种线程,一种是用户线程,另一种是守护线程.守护线程是一种特殊的线程,当进程中不存在非守护线程了,则守护线程自动销毁.典型的守护线程就是垃圾回收线程.用这个比较通俗的比喻来解释一下"守护线程":任何一个守护线程都是整个JVM中所有非守护线程的保姆,只要当前的JVM实例中存在任何一个非守护线程没有结束,守护线程就在工作,只有当最后一个非守护线程结束时,守护线程才随着JVM一同结束工作.Daemon的作用就是为其它线程的运行提供便利服务,守护线程最典型的应用就是GC,它是一个很称职的守护者.
public class DaemonThread extends Thread{
private static int count = 0;
@Override
public void run() {
while(true) {
try {
count ++;
System.out.println(count);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
DaemonThread thread = new DaemonThread();
thread.setDaemon(true);
thread.start();
Thread.sleep(5000);
System.out.println("main thread is exist, so daemon is stop working");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}