在Thread类中,定义了三个静态值来表示线程的优先级:
MAX_PRIORITY
这是最高的线程优先级,值为10。
NORM_PRIORITY
这是默认的线程优先级,值为5。
MIN_PRIORITY
这是最低的线程优先级,值为1。
语法
public final int getPriority()
登录后复制
Example
public class ThreadPriorityTest extends Thread {
public static void main(String[]args) {
ThreadPriorityTest thread1 = new ThreadPriorityTest();
ThreadPriorityTest thread2 = new ThreadPriorityTest();
ThreadPriorityTest thread3 = new ThreadPriorityTest();
System.out.println("Default thread priority of thread1: " + thread1.getPriority());
System.out.println("Default thread priority of thread2: " + thread2.getPriority());
System.out.println("Default thread priority of thread3: " + thread3.getPriority());
thread1.setPriority(8);
thread2.setPriority(3);
thread3.setPriority(6);
System.out.println("New thread priority of thread1: " + thread1.getPriority());
System.out.println("New thread priority of thread2: " + thread2.getPriority());
System.out.println("New thread priority of thread3: " + thread3.getPriority());
}
}
登录后复制
输出
Default thread priority of thread1: 5
Default thread priority of thread2: 5
Default thread priority of thread3: 5
New thread priority of thread1: 8
New thread priority of thread2: 3
New thread priority of thread3: 6
登录后复制
以上就是在Java中,线程优先级的重要性是什么?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!