• JAVA面向对象学习——java多线程———控制线程——线程优先级


     

    ============================================================================

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

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

    Thread提供了setPriority(int newPriority)和getPriority()方法来设置和返回指定线程的优先级,其中setPriority方法的参数可以是一个整数,范围是1~10之间,

    也可以使Thread类的三个静态常量: MAX_PRIORITY:其值是10。 MIN_PRIORITY:其值是1。 NORM_PRIORITY:其值是5。

    ==========================================================

    改变线程优先级


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

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

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

    也可以使用Thread类的如下三个静态常量。

    ➢ MAX_PRIORITY:其值是10。

    ➢ MIN_PRIORITY:其值是1。

    ➢ NORM_PRIORITY:其值是5。

    ==================================================================

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

      

    public class PriorityTest extends Thread
    {
        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
    低级-------1,----------2
    低级-------1,----------3
    创建之初的优先级:6
    低级-------1,----------4
    高级-------10,----------0
    低级-------1,----------5
    高级-------10,----------1
    低级-------1,----------6
    高级-------10,----------2
    低级-------1,----------7
    高级-------10,----------3
    低级-------1,----------8
    高级-------10,----------4
    低级-------1,----------9
    高级-------10,----------5
    低级-------1,----------10
    低级-------1,----------11
    高级-------10,----------6
    低级-------1,----------12
    低级-------1,----------13
    低级-------1,----------14
    低级-------1,----------15
    高级-------10,----------7
    低级-------1,----------16
    高级-------10,----------8
    低级-------1,----------17
    高级-------10,----------9
    低级-------1,----------18
    高级-------10,----------10
    低级-------1,----------19
    高级-------10,----------11
    低级-------1,----------20
    高级-------10,----------12
    低级-------1,----------21
    高级-------10,----------13
    低级-------1,----------22
    高级-------10,----------14
    低级-------1,----------23
    高级-------10,----------15
    低级-------1,----------24
    高级-------10,----------16
    低级-------1,----------25
    高级-------10,----------17
    低级-------1,----------26
    高级-------10,----------18
    低级-------1,----------27
    低级-------1,----------28
    高级-------10,----------19
    低级-------1,----------29
    高级-------10,----------20
    低级-------1,----------30
    高级-------10,----------21
    低级-------1,----------31
    高级-------10,----------22
    低级-------1,----------32
    高级-------10,----------23
    低级-------1,----------33
    高级-------10,----------24
    低级-------1,----------34
    高级-------10,----------25
    低级-------1,----------35
    高级-------10,----------26
    低级-------1,----------36
    高级-------10,----------27
    低级-------1,----------37
    高级-------10,----------28
    低级-------1,----------38
    高级-------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,----------39
    低级-------1,----------40
    低级-------1,----------41
    低级-------1,----------42
    低级-------1,----------43
    低级-------1,----------44
    低级-------1,----------45
    低级-------1,----------46
    低级-------1,----------47
    低级-------1,----------48
    低级-------1,----------49

  • 相关阅读:
    java.util.Date和java.sql.Date的区别及应用
    powderdesinger显示中英文表名
    Axure实现提示文本单击显示后自动消失的效果
    如何把Java的double类型变量保留两位小数
    MyBatis查询,返回值Map或List<Map>
    Spring Security整合JWT,实现单点登录,So Easy~!
    win10+mysql8.0安装
    Intellij IDEA导入JAVA项目并启动(哈哈哈,天天都有人问)
    色彩设计基础知识整理
    前端开发者必备的Nginx知识
  • 原文地址:https://www.cnblogs.com/xiaobaibailongma/p/16064049.html
Copyright © 2020-2023  润新知