我目前正在学习Java线程,最近尝试了以下代码。但是即使我设置了优先级,输出也似乎没有遵循任何顺序:
class head implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getPriority());
}
}
class Main {
public static void main(String[] args) {
Thread obj1 = new Thread(new head());
Thread obj2 = new Thread(new head());
Thread obj3 = new Thread(new head());
Thread obj4 = new Thread(new head());
Thread obj5 = new Thread(new head());
obj1.setPriority(1);
obj2.setPriority(2);
obj3.setPriority(3);
obj4.setPriority(4);
obj5.setPriority(5);
obj1.start();
obj2.start();
obj3.start();
obj4.start();
obj5.start();
}
}
如果有人可以帮助我解决此问题,并使其以正确的顺序显示(通过设置优先级方法设置),将不胜感激,谢谢您的光临。