线程优先级

2021-03-09 17:30

阅读:615

标签:orm   pac   xtend   like   min   contain   cpu   状态   thread   

# 线程优先级

  1. 线程优先级

    Java提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程,线程调度器按照优先级决定应该调用哪个线程来执行

  2. 线程的优先级用数字表示,范围1~10

    Thread.MIN_PRIORITY = 1;

    Thread.MAX_PRIMORITY = 10;

    Thread.NORM_PRIMORITY = 5;

  3. 使用getPriority().setPriority(int xxx)来获取线程优先级

  4. 优先级的设定建议在start()调度前

  5. 优先级低是意味着获得调度的概率低,并不是优先级低就不会被调用了,这都是看cpu的调度。

    package Thread;
    ?
    public class PriorityThread extends Thread {
      public static void main(String[] args) {
          System.out.println(Thread.currentThread().getName() + "---->" + Thread.currentThread().getPriority());
          MyPrioryity myPrioryity = new MyPrioryity();
          Thread t1 = new Thread(myPrioryity,"线程1");
          Thread t2 = new Thread(myPrioryity,"线程2");
          Thread t3 = new Thread(myPrioryity,"线程3");
          Thread t4 = new Thread(myPrioryity,"线程4");
          Thread t5 = new Thread(myPrioryity,"线程5");
          Thread t6 = new Thread(myPrioryity,"线程6");
          t1.start();
          t2.setPriority(1);
          t2.start();
          t3.setPriority(4);
          t3.start();
          t4.setPriority(Thread.MAX_PRIORITY);//Max_PRIORITY = 10
          t4.start();
          t5.setPriority(7);
          t5.start();
          t6.setPriority(8);
          t6.start();
      }
    }
    ?
    class MyPrioryity implements Runnable {
    ?
      @Override
      public void run() {
          System.out.println(Thread.currentThread().getName() + "---->" + Thread.currentThread().getPriority());
      }
    }

     

线程优先级

标签:orm   pac   xtend   like   min   contain   cpu   状态   thread   

原文地址:https://www.cnblogs.com/QIDream/p/14171253.html

上一篇:Java Web

下一篇:C++动态数组


评论


亲,登录后才可以留言!