主线程优先级为5,新创建的线程的优先级与主线程的优先级相同,但是在启动线程之前,我将新创建的线程的优先级更改为10.优先级最高的线程需要先执行,但是没有像这样发生,主线程首先执行。请告诉我代码中的原因和错误,我粘贴了总代码和输出。请帮帮我。
public class MyThread1 extends Thread{
@Override
public void run(){
System.out.println("Child Thread............");
for(int i=1;i<10;i++){
System.out.println("Child Thread");
}
}
}
public class ThreadMain {
public static void main(String ar[]){
int mainPriority=Thread.currentThread().getPriority();
System.out.println("mainPriority = " + mainPriority);
MyThread1 t1=new MyThread1();
t1.setPriority(10);
t1.start();
int childPriority=t1.getPriority();
System.out.println("childPriority = " + childPriority);
for(int i=1;i<10;i++){
System.out.println("Main Thread");
}
}
}
输出如下?
mainPriority = 5
childPriority = 10
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Child Thread............
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
任何人都可以帮忙,谢谢
答案 0 :(得分:1)
线程优先级仅仅是对底层操作系统(调度程序)的建议/请求 - 您能否以更高的优先级运行此线程?。操作系统可能只是决定忽略您的请求
答案 1 :(得分:0)
没有首先执行最优先级的线程。