多线程中的Java线程优先级

正如我们已经知道的那样,java完全面向对象,可以在 多线程环境 在哪儿 线程调度器 根据线程的优先级将处理器分配给线程。每当我们在Java中创建一个线程时,它总是有一些优先级分配给它。优先级可以由JVM在创建线程时给出,也可以由程序员显式地给出。

null

线程中的优先级 是一个概念,其中每个线程都有一个优先级,在外行的语言中,可以说每个对象都有优先级,它由1到10的数字表示。

  • 默认优先级设置为5(例外)。
  • 最小优先级设置为1。
  • 最高优先级设置为10。

其中定义了3个常数,即:

  1. 公共静态int_优先级
  2. 公共静态最小优先级
  3. 公共静态整数最大优先级

让我们用一个例子来讨论这项工作是如何在内部执行的。在这里,我们将使用上面收集的知识,如下所示:

线程的可接受优先级值在1到10之间。

让我们讨论一下如何在java中获取和设置线程的优先级。

  1. public final int getPriority(): JAVA朗。丝线。getPriority()方法返回给定线程的优先级。
  2. 公共最终无效设置优先级(int newPriority): JAVA朗。丝线。方法将线程的优先级更改为值newPriority。如果参数newPriority的值超出最小(1)和最大(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());
}
}


输出

t1 thread priority : 5t2 thread priority : 5t3 thread priority : 5t1 thread priority : 2t2 thread priority : 5t3 thread priority : 8Currently Executing Thread : mainMain thread priority : 5Main 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 : 6t1 thread priority : 6

输出说明:

  • 如果两个线程具有相同的优先级,那么我们不能期望哪个线程将首先执行。这取决于线程调度程序的算法(循环、先到先得等)
  • 如果我们使用线程优先级进行线程调度,那么我们应该始终记住,底层平台应该支持基于线程优先级的调度。

所有这些处理都是在线程调度程序的帮助下进行的,通过以下提供的视频样本,线程调度程序可以更好地可视化:

本文由 达尔梅什·辛格 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享