您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

多线程中的Java线程优先级

2024/8/18 5:23:05发布48次查看
在多线程的情况下,线程调度程序根据不同的条件将线程分配给特定的进程。他们的优先事项。 java 线程具有预先分配的优先级。除此之外,java虚拟机器还可以为线程分配优先级或由程序员明确指定。范围为线程优先级的值介于 1 到 10(含)之间。三个静态变量与优先级相关的有 -
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线程优先级的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product