admin管理员组

文章数量:1624317

线程的优先级(优先级(priority)要设置在.start之前)

 

  1. 线程的默认优先级为5(例如主线程main的),最低的优先级为1,最大的优先级为10.其中若是大于最大的优先级10或者小于最小的优先级1。他会抛出异常不会执行
  2. 优先级低cpu可能会发生先与优先级高的调度

 

 

还是要看cpu心情(麻了)

 

package org.example.threadpriority;

public class TestPriority {
    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName()+"--->"+Thread.currentThread().getPriority());
        Priority priority = new Priority();
        Thread thread1 = new Thread(priority);
        Thread thread2 = new Thread(priority);
        Thread thread3 = new Thread(priority);
        Thread thread4 = new Thread(priority);
        Thread thread5 = new Thread(priority);
        Thread thread6 = new Thread(priority);


        thread1.start();

        thread2.setPriority(1);
        thread2.start();

        thread3.setPriority(4);
        thread3.start();

        thread4.setPriority(Thread.MAX_PRIORITY);
        thread4.start();

//        thread5.setPriority(11);
        thread5.setPriority(8);
        thread5.start();

//        thread6.setPriority(-1);
        thread6.setPriority(6);
        thread6.start();


    }
}
class Priority implements Runnable{

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



本文标签: 优先级线程Prioritystart