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

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

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());
    }


}

priority


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

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

priority

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

helloses

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值