多线程中的Java线程优先级
正如我们已经知道的那样, Java完全面向对象在多线程环境中工作,其中线程调度程序根据线程的优先级将处理器分配给线程。每当我们在Java中创建一个线程时,它总是被分配一些优先级。优先级可以在创建线程时由 JVM 给出,也可以由程序员明确给出。
线程中的优先级是一个概念,其中每个线程都有一个优先级,用外行的语言可以说每个对象在这里都有优先级,由 1 到 10 的数字表示。
- 默认优先级设置为 5 作为例外。
- 最低优先级设置为 1。
- 最大优先级设置为 10。
这里定义了3个常量,如下所示:
- 公共静态 int NORM_PRIORITY
- 公共静态 int MIN_PRIORITY
- 公共静态 int MAX_PRIORITY
让我们用一个例子来讨论它,以了解工作是如何在内部执行的。在这里,我们将使用上面收集的知识如下:
- 我们将使用currentThread()方法来获取当前线程的名称。用户也可以使用setName()方法,如果他/她想根据选择来命名线程以便理解。
- getName()方法将用于获取线程的名称。
The accepted value of priority for a thread is in the range of 1 to 10.
让我们讨论如何在Java中获取和设置线程的优先级。
- public final int getPriority(): Java.lang.Thread.getPriority() 方法返回给定线程的优先级。
- public final void setPriority(int newPriority): Java.lang.Thread.setPriority() 方法将线程的优先级更改为值newPriority。如果参数 newPriority 的值超出 minimum(1) 和 maximum(10) 限制,此方法将引发 IllegalArgumentException。
例子
Java
// Java Program to Illustrate Priorities in Multithreading
// via help of getPriority() and setPriority() method
// Importing required classes
import java.lang.*;
// Main class
class ThreadDemo extends Thread {
// Method 1
// run() method for the thread that is called
// as soon as start() is invoked for thread in main()
public void run()
{
// Print statement
System.out.println("Inside run method");
}
// Main driver method
public static void main(String[] args)
{
// Creating random threads
// with the help of above class
ThreadDemo t1 = new ThreadDemo();
ThreadDemo t2 = new ThreadDemo();
ThreadDemo t3 = new ThreadDemo();
// Thread 1
// Display the priority of above thread
// using getPriority() method
System.out.println("t1 thread priority : "
+ t1.getPriority());
// Thread 1
// Display the priority of above thread
System.out.println("t2 thread priority : "
+ t2.getPriority());
// Thread 3
System.out.println("t3 thread priority : "
+ t3.getPriority());
// Setting priorities of above threads by
// passing integer arguments
t1.setPriority(2);
t2.setPriority(5);
t3.setPriority(8);
// t3.setPriority(21); will throw
// IllegalArgumentException
// 2
System.out.println("t1 thread priority : "
+ t1.getPriority());
// 5
System.out.println("t2 thread priority : "
+ t2.getPriority());
// 8
System.out.println("t3 thread priority : "
+ t3.getPriority());
// Main thread
// Displays the name of
// currently executing Thread
System.out.println(
"Currently Executing Thread : "
+ Thread.currentThread().getName());
System.out.println(
"Main thread priority : "
+ Thread.currentThread().getPriority());
// Main thread priority is set to 10
Thread.currentThread().setPriority(10);
System.out.println(
"Main thread priority : "
+ Thread.currentThread().getPriority());
}
}
Java
// Java program to demonstrate that a Child thread
// Getting Same Priority as Parent thread
// Importing all classes from java.lang package
import java.lang.*;
// Main class
// ThreadDemo
// Extending Thread class
class GFG extends Thread {
// Method 1
// run() method for the thread that is
// invoked as threads are started
public void run()
{
// Print statement
System.out.println("Inside run method");
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// main thread priority is set to 6 now
Thread.currentThread().setPriority(6);
// Current thread is accessed
// using currentThread() method
// Print and display main thread priority
// using getPriority() method of Thread class
System.out.println(
"main thread priority : "
+ Thread.currentThread().getPriority());
// Creating a thread by creating object inside
// main()
GFG t1 = new GFG();
// t1 thread is child of main thread
// so t1 thread will also have priority 6
// Print and display priority of current thread
System.out.println("t1 thread priority : "
+ t1.getPriority());
}
}
输出
t1 thread priority : 5
t2 thread priority : 5
t3 thread priority : 5
t1 thread priority : 2
t2 thread priority : 5
t3 thread priority : 8
Currently Executing Thread : main
Main thread priority : 5
Main thread priority : 10
输出说明:
- 具有最高优先级的线程将先于其他线程获得执行机会。假设有 3 个线程 t1、t2 和 t3,优先级分别为 4、6 和 1。因此,线程 t2 将根据最大优先级 6 首先执行,然后执行 t1,然后执行 t3。
- 主线程的默认优先级始终为 5,以后可以更改。所有其他线程的默认优先级取决于父线程的优先级。
现在极客们一定想知道如果我们为线程分配相同的优先级会发生什么。为了照顾线程而进行的所有处理都是在线程调度程序的帮助下进行的。如果优先级设置为相同,可以参考下面的示例,稍后我们将讨论它作为输出解释,以便在概念上和实践上更好地理解。
例子
Java
// Java program to demonstrate that a Child thread
// Getting Same Priority as Parent thread
// Importing all classes from java.lang package
import java.lang.*;
// Main class
// ThreadDemo
// Extending Thread class
class GFG extends Thread {
// Method 1
// run() method for the thread that is
// invoked as threads are started
public void run()
{
// Print statement
System.out.println("Inside run method");
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// main thread priority is set to 6 now
Thread.currentThread().setPriority(6);
// Current thread is accessed
// using currentThread() method
// Print and display main thread priority
// using getPriority() method of Thread class
System.out.println(
"main thread priority : "
+ Thread.currentThread().getPriority());
// Creating a thread by creating object inside
// main()
GFG t1 = new GFG();
// t1 thread is child of main thread
// so t1 thread will also have priority 6
// Print and display priority of current thread
System.out.println("t1 thread priority : "
+ t1.getPriority());
}
}
输出
main thread priority : 6
t1 thread priority : 6
输出说明:
- 如果两个线程具有相同的优先级,那么我们不能指望哪个线程将首先执行。这取决于线程调度程序的算法(循环、先到先服务等)
- 如果我们使用线程优先级进行线程调度,那么我们应该始终牢记,底层平台应该提供基于线程优先级的调度支持。
所有这些处理都是在线程调度程序的帮助下进行的,在下面提供的视频示例的帮助下可以更好地可视化,如下所示: