线程设置优先级与性能倒置

线程设置优先级与性能倒置

priority设置优先级0~10,默认5

超过报错:ThreadStateException


应用场景:游戏中同时按下大中小招数时,系统优先释放谁

package com.li.changGe.multithreading.threadState;

public class PriorityDemo01 implements Runnable{
  public static void main(String[] args) {
      /**
       * 给普攻,招数和必杀技设置优先级
       * 启动线程
       */
      PriorityDemo01 priorityDemo01 = new PriorityDemo01();

      Thread ordinary = new Thread(priorityDemo01,"1");
      Thread tricks = new Thread(priorityDemo01,"2");
      Thread nirvana = new Thread(priorityDemo01,"3");

      /**
       * 先设置优先级再运行
       *
       * NORM_PRIORITY = 5
       */
    nirvana.setPriority(Thread.MAX_PRIORITY);
    ordinary.setPriority(Thread.MIN_PRIORITY);
    tricks.setPriority(Thread.NORM_PRIORITY);

    tricks.start();
    ordinary.start();
    nirvana.start();

  }

    @Override
    public void run() {
      System.out.println(Thread.currentThread().getName()+":"
          +Thread.currentThread().getPriority());
    }


}

线程设置优先级与性能倒置_第1张图片


性能倒置:cpu有时并不一定就会按照优先级来调度

原本优先级高的线程却后执行

线程设置优先级与性能倒置_第2张图片

你可能感兴趣的:(java基础,代理模式,udp,网络)