max_priority - 线程拥有的最大优先级,默认值为 10。
norm_priority - 线程具有的默认优先级,默认值为 5。
min_priority - 线程具有的最小优先级,默认值为 1。
java 中的“getpriority()”方法有助于返回绑定为值的线程优先级。
“setpriority()”方法更改给定线程的优先级值。它抛出当线程优先级小于 1 或大于 10 时,出现 illegalargumentexception。
示例 实时演示
import java.lang.*;public class demo extends thread{ public void run(){ system.out.println("now, inside the run method"); } public static void main(string[]args){ demo my_thr_1 = new demo(); demo my_thr_2 = new demo(); system.out.println("the thread priority of first thread is : " + my_thr_1.getpriority()); system.out.println("the thread priority of first thread is : " + my_thr_2.getpriority()); my_thr_1.setpriority(5); my_thr_2.setpriority(3); system.out.println("the thread priority of first thread is : " + my_thr_1.getpriority()); system.out.println("the thread priority of first thread is : " + my_thr_2.getpriority()); system.out.print(thread.currentthread().getname()); system.out.println("the thread priority of main thread is : " + thread.currentthread().getpriority()); thread.currentthread().setpriority(10); system.out.println("the thread priority of main thread is : " + thread.currentthread().getpriority()); }}
输出the thread priority of first thread is : 5the thread priority of first thread is : 5the thread priority of first thread is : 5the thread priority of first thread is : 3the thread priority of main thread is : 5the thread priority of main thread is : 10
名为 demo 的类继承自基类 thread。函数‘run’被定义并且相关消息被定义。在 main 函数中,创建了 demo 类的两个实例,并将它们优先级是通过调用函数“getpriority”找到的。
它们被打印在控制台上。接下来,使用以下方法为 demo 实例分配优先级:‘设置优先级’函数。输出显示在控制台上。打印线程的名称借助“getname”功能在屏幕上显示。
以上就是多线程中的java线程优先级的详细内容。