java多线程(16)--线程控制之线程优先级

每个线程执行时都具有一定的优先级,优先级高的线程获得较多的执行机会,而优先级低的线程则获得较少的执行机会。

每个线程默认的优先级都与创建它的父线程的优先级相同,在默认情况下,main线程具有普通优先级,由main线程创建的子线程也具有普通优先级。

Thread类提供了setPriority(int newPriority)、getPriority()方法来设置和返回指定线程的优先级,其中setPriority()方法的参数可以是一个整数,范围是1~10之间,也可以使用Thread类的如下三个静态常量。

A、MAX_PRIORITY:其值是10。

B、MIN_PRIORITY:其值是1。

C、NORM_PRIORITY:其值是5。

package hlztest;

public class PriorityTest extends Thread{  
    //定义一个有参构造器函数,用于创建线程时指定name  
    public PriorityTest(String name){  
        super(name);  
    }  
      
    public void run(){  
        for(int i=0;i<50;i++){  
            System.out.println(getName()+",其优先级是:"+getPriority()+",循环变量的值为:"+i);  
        }  
    }  
      
    public static void main(String[] args) {  
        //改变主线程的优先级  
        Thread.currentThread().setPriority(6);  
        for(int i=0;i<30;i++){  
            if(i==10){  
                PriorityTest low=new PriorityTest("低级");  
                low.start();  
                System.out.println("创建之初的优先级:"+low.getPriority());  
                //设置该线程为最低优先级  
                low.setPriority(Thread.MIN_PRIORITY);  
            }  
            if(i==20){  
                PriorityTest high=new PriorityTest("高级");  
                high.start();  
                System.out.println("创建之初的优先级:"+high.getPriority());  
                //设置该线程为最低优先级  
                high.setPriority(Thread.MAX_PRIORITY);  
            }  
        }  
    }  
}  


创建之初的优先级:6
低级,其优先级是:6,循环变量的值为:0
低级,其优先级是:1,循环变量的值为:1
高级,其优先级是:6,循环变量的值为:0
高级,其优先级是:6,循环变量的值为:1
创建之初的优先级:6
低级,其优先级是:1,循环变量的值为:2
高级,其优先级是:6,循环变量的值为:2
高级,其优先级是:10,循环变量的值为:3
高级,其优先级是:10,循环变量的值为:4
高级,其优先级是:10,循环变量的值为:5
高级,其优先级是:10,循环变量的值为:6
高级,其优先级是:10,循环变量的值为:7
高级,其优先级是:10,循环变量的值为:8
高级,其优先级是:10,循环变量的值为:9
高级,其优先级是:10,循环变量的值为:10
高级,其优先级是:10,循环变量的值为:11
高级,其优先级是:10,循环变量的值为:12
高级,其优先级是:10,循环变量的值为:13
高级,其优先级是:10,循环变量的值为:14
高级,其优先级是:10,循环变量的值为:15
高级,其优先级是:10,循环变量的值为:16
高级,其优先级是:10,循环变量的值为:17
高级,其优先级是:10,循环变量的值为:18
高级,其优先级是:10,循环变量的值为:19
高级,其优先级是:10,循环变量的值为:20
高级,其优先级是:10,循环变量的值为:21
高级,其优先级是:10,循环变量的值为:22
高级,其优先级是:10,循环变量的值为:23
高级,其优先级是:10,循环变量的值为:24
高级,其优先级是:10,循环变量的值为:25
高级,其优先级是:10,循环变量的值为:26
高级,其优先级是:10,循环变量的值为:27
高级,其优先级是:10,循环变量的值为:28
高级,其优先级是:10,循环变量的值为:29
高级,其优先级是:10,循环变量的值为:30
高级,其优先级是:10,循环变量的值为:31
高级,其优先级是:10,循环变量的值为:32
高级,其优先级是:10,循环变量的值为:33
高级,其优先级是:10,循环变量的值为:34
高级,其优先级是:10,循环变量的值为:35
高级,其优先级是:10,循环变量的值为:36
高级,其优先级是:10,循环变量的值为:37
高级,其优先级是:10,循环变量的值为:38
高级,其优先级是:10,循环变量的值为:39
高级,其优先级是:10,循环变量的值为:40
高级,其优先级是:10,循环变量的值为:41
高级,其优先级是:10,循环变量的值为:42
高级,其优先级是:10,循环变量的值为:43
高级,其优先级是:10,循环变量的值为:44
高级,其优先级是:10,循环变量的值为:45
高级,其优先级是:10,循环变量的值为:46
高级,其优先级是:10,循环变量的值为:47
高级,其优先级是:10,循环变量的值为:48
高级,其优先级是:10,循环变量的值为:49
低级,其优先级是:1,循环变量的值为:3
低级,其优先级是:1,循环变量的值为:4
低级,其优先级是:1,循环变量的值为:5
低级,其优先级是:1,循环变量的值为:6
低级,其优先级是:1,循环变量的值为:7
低级,其优先级是:1,循环变量的值为:8
低级,其优先级是:1,循环变量的值为:9
低级,其优先级是:1,循环变量的值为:10
低级,其优先级是:1,循环变量的值为:11
低级,其优先级是:1,循环变量的值为:12
低级,其优先级是:1,循环变量的值为:13
低级,其优先级是:1,循环变量的值为:14
低级,其优先级是:1,循环变量的值为:15
低级,其优先级是:1,循环变量的值为:16
低级,其优先级是:1,循环变量的值为:17
低级,其优先级是:1,循环变量的值为:18
低级,其优先级是:1,循环变量的值为:19
低级,其优先级是:1,循环变量的值为:20
低级,其优先级是:1,循环变量的值为:21
低级,其优先级是:1,循环变量的值为:22
低级,其优先级是:1,循环变量的值为:23
低级,其优先级是:1,循环变量的值为:24
低级,其优先级是:1,循环变量的值为:25
低级,其优先级是:1,循环变量的值为:26
低级,其优先级是:1,循环变量的值为:27
低级,其优先级是:1,循环变量的值为:28
低级,其优先级是:1,循环变量的值为:29
低级,其优先级是:1,循环变量的值为:30
低级,其优先级是:1,循环变量的值为:31
低级,其优先级是:1,循环变量的值为:32
低级,其优先级是:1,循环变量的值为:33
低级,其优先级是:1,循环变量的值为:34
低级,其优先级是:1,循环变量的值为:35
低级,其优先级是:1,循环变量的值为:36
低级,其优先级是:1,循环变量的值为:37
低级,其优先级是:1,循环变量的值为:38
低级,其优先级是:1,循环变量的值为:39
低级,其优先级是:1,循环变量的值为:40
低级,其优先级是:1,循环变量的值为:41
低级,其优先级是:1,循环变量的值为:42
低级,其优先级是:1,循环变量的值为:43
低级,其优先级是:1,循环变量的值为:44
低级,其优先级是:1,循环变量的值为:45
低级,其优先级是:1,循环变量的值为:46
低级,其优先级是:1,循环变量的值为:47
低级,其优先级是:1,循环变量的值为:48
低级,其优先级是:1,循环变量的值为:49



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值