
/*
  线程的优先级:
  1.
  MAX_PRIORITY:10
  MIN_PRIORITY:1
  NORM_PRIORITY:5  —>默认优先级
 
  2.如何获取和设置当前线程的优先级:
    getPriority():获取线程的优先级
    setPriority(int p):设置线程的优先级
 
    说明:高优先级的线程要抢占低优先级的线程CPU的执行权。但是只是从概率上讲,高优先级的线程
          高概率的情况下被执行。并不意味着只有当高优先级的线程执行完以后,低优先级的线程才
          执行。
 
 /
package com.atguigu.java1;/*** 线程的优先级:* 1.* MAX_PRIORITY:10* MIN_PRIORITY:1* NORM_PRIORITY:5 -->默认优先级** 2.如何获取和设置当前线程的优先级:* getPriority():获取线程的优先级* setPriority(int p):设置线程的优先级** 说明:高优先级的线程要抢占低优先级的线程CPU的执行权。但是只是从概率上讲,高优先级的线程* 高概率的情况下被执行。并不意味着只有当高优先级的线程执行完以后,低优先级的线程才* 执行。** @author Dxkstart* @create 2021-05-06 16:14*/public class PriorityTest {public static void main(String[] args) {MyThread2 my = new MyThread2();//设置线程优先级my.setPriority(8);//注意格式my.start();//分线程1//如下的方法仍然是main线程中执行的Thread.currentThread().setPriority(2);for (int i = 0; i <= 200; i++) {if (i % 2 == 0) {System.out.println(Thread.currentThread().getName() +":"+Thread.currentThread().getPriority() +":" + i );}}}}class MyThread2 extends Thread {@Overridepublic void run() {for (int i = 0; i <= 200; i++) {if (i % 2 == 0) {System.out.println(Thread.currentThread().getName() +":"+Thread.currentThread().getPriority() + ":" + i);}}}}
