Java线程优先级

前言

 在操作系统中,线程可以划分优先级,优先级较高的得到CPU的资源越多,也就是CPU先执行优先级较高的线程对象中的任务.
 在Java中,线程的优先级分为1-10个等级,数字越大,优先级越高.如果不在这个范围内,则会抛出IllegalArgumentException异常.
 Java优先级具有继承性,子线程会获得和父线程同样的优先级等级.
 线程的优先级高度依赖系统,当虚拟机依赖于宿主平台的线程实现机制时,Java的线程优先级被映射到宿主平台的优先级等级中.优先级个数可能更多,也可能更少.

Java中的优先级

Java中的优先级定义在java.lang.Thread中

//优先级属性
private int            priority;
//最小的优先级等级
public static final int MIN_PRIORITY = 1;
//默认的优先级等级
public static final int NORM_PRIORITY = 5;
//最大的优先级等级
public static final int MAX_PRIORITY = 10;

设置优先级等级,最后调用的是c/c++编写的底层函数,应该是映射到操作系统的优先级等级.

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();
        }
        //调用的是c/c++编写的底层函数
        setPriority0(priority = newPriority);
    }
}

实例

package org.prior;

public class Prior {

    public static void main(String[] args) {
        System.out.println("线程优先级测试");
        PriorThread1 th1 = new PriorThread1();
        PriorThread2 th2 = new PriorThread2();

        //线程th1的优先级较低
        th1.setPriority(5);
        th2.setPriority(6);     

        th1.start();
        th2.start();
    }

}

class PriorThread1 extends Thread {

    @Override
    public void run() {
        for(int i = 0;i < 50 ;i++ ) {
            System.out.print(Thread.currentThread().getName() + "  " + i + "\t");
            if(i % 10 == 0 ) {
                System.out.println();
            }
        }
    }

}

class PriorThread2 extends Thread{
    @Override
    public void run() {
        for(int i = 0;i < 50 ;i++ ) {
            System.out.print(Thread.currentThread().getName() + "  " + i + "\t");
            if(i % 10 == 0 ) {
                System.out.println();
            }
        }
    }
}

测试结果

线程优先级测试
Thread-0  0 
Thread-1  0 Thread-0  1 Thread-0  2 Thread-0  3 
Thread-0  4 Thread-1  1 Thread-1  2 Thread-1  3 Thread-1  4 Thread-1  5 Thread-1  6 Thread-1  7 Thread-1  8 Thread-0  5 Thread-1  9 Thread-0  6 Thread-0  7 Thread-0  8 Thread-0  9 Thread-1  10    
Thread-1  11    Thread-1  12    Thread-1  13    Thread-1  14    Thread-1  15    Thread-1  16    Thread-1  17    Thread-1  18    Thread-1  19    Thread-1  20    
Thread-1  21    Thread-1  22    Thread-1  23    Thread-1  24    Thread-1  25    Thread-1  26    Thread-1  27    Thread-1  28    Thread-1  29    Thread-1  30    
Thread-1  31    Thread-1  32    Thread-1  33    Thread-1  34    Thread-1  35    Thread-1  36    Thread-1  37    Thread-1  38    Thread-1  39    Thread-1  40    
Thread-1  41    Thread-1  42    Thread-1  43    Thread-1  44    Thread-1  45    Thread-1  46    Thread-1  47    Thread-1  48    Thread-1  49    Thread-0  10    
Thread-0  11    Thread-0  12    Thread-0  13    Thread-0  14    Thread-0  15    Thread-0  16    Thread-0  17    Thread-0  18    Thread-0  19    Thread-0  20    
Thread-0  21    Thread-0  22    Thread-0  23    Thread-0  24    Thread-0  25    Thread-0  26    Thread-0  27    Thread-0  28    Thread-0  29    Thread-0  30    
Thread-0  31    Thread-0  32    Thread-0  33    Thread-0  34    Thread-0  35    Thread-0  36    Thread-0  37    Thread-0  38    Thread-0  39    Thread-0  40    
Thread-0  41    Thread-0  42    Thread-0  43    Thread-0  44    Thread-0  45    Thread-0  46    Thread-0  47    Thread-0  48    Thread-0  49    

Thread-0的优先级比Thread-1的优先级低,因此Thread-1获得的运行机会是比较高的,在多次测试过程中,Thread-1都是先运行完.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值