Java 通过设置优先级的线程执行顺序
我已按以下顺序设置线程的优先级 但当我在程序下面运行时,有时B在A之前运行。 我不理解这个执行,因为我设置了B的优先级低于A的优先级Java 通过设置优先级的线程执行顺序,java,multithreading,Java,Multithreading,我已按以下顺序设置线程的优先级 但当我在程序下面运行时,有时B在A之前运行。 我不理解这个执行,因为我设置了B的优先级低于A的优先级 public class AThread implements Runnable{ public void run(){ System.out.println("In thread A"); }} public class BThread implements Runnable { public void run(){
public class AThread implements Runnable{
public void run(){
System.out.println("In thread A");
}}
public class BThread implements Runnable {
public void run(){
System.out.println("In thread B");
}
}
public class CThread implements Runnable {
public void run(){
System.out.println("In thread C");
}
}
public class ThreadPriorityDemo {
public static void main(String args[]){
AThread A = new AThread();
Thread tA = new Thread(A);
BThread B = new BThread();
Thread tB = new Thread(B);
CThread C = new CThread();
Thread tC = new Thread(C);
tA.setPriority(Thread.MAX_PRIORITY);
tC.setPriority(Thread.MIN_PRIORITY);
tB.setPriority(tA.getPriority() -1);
System.out.println("A started");
tA.start();
System.out.println("B started");
tB.start();
System.out.println("C started");
tC.start();
}
}线程优先级可能不是您想象的那样 线程的优先级是操作系统在涉及这两个线程的任何调度或CPU分配决策点上优先选择一个线程的建议。但是如何实现这一点取决于操作系统和JVM实现 对线程优先级进行了很好的讨论。要点是:
我认为您的问题可能源于上述第三点(如果您在Windows上运行),但也可能是其他原因。我认为正确的答案是:您无法通过设置线程优先级来可靠地排序线程启动
public class AThread implements Runnable{
public void run(){
System.out.println("In thread A");
}}
public class BThread implements Runnable {
public void run(){
System.out.println("In thread B");
}
}
public class CThread implements Runnable {
public void run(){
System.out.println("In thread C");
}
}
public class ThreadPriorityDemo {
public static void main(String args[]){
AThread A = new AThread();
Thread tA = new Thread(A);
BThread B = new BThread();
Thread tB = new Thread(B);
CThread C = new CThread();
Thread tC = new Thread(C);
tA.setPriority(Thread.MAX_PRIORITY);
tC.setPriority(Thread.MIN_PRIORITY);
tB.setPriority(tA.getPriority() -1);
System.out.println("A started");
tA.start();
System.out.println("B started");
tB.start();
System.out.println("C started");
tC.start();
}
我认为你的困惑源于美国
优先级较高的线程优先于线程执行
优先级较低
public class AThread implements Runnable{
public void run(){
System.out.println("In thread A");
}}
public class BThread implements Runnable {
public void run(){
System.out.println("In thread B");
}
}
public class CThread implements Runnable {
public void run(){
System.out.println("In thread C");
}
}
public class ThreadPriorityDemo {
public static void main(String args[]){
AThread A = new AThread();
Thread tA = new Thread(A);
BThread B = new BThread();
Thread tB = new Thread(B);
CThread C = new CThread();
Thread tC = new Thread(C);
tA.setPriority(Thread.MAX_PRIORITY);
tC.setPriority(Thread.MIN_PRIORITY);
tB.setPriority(tA.getPriority() -1);
System.out.println("A started");
tA.start();
System.out.println("B started");
tB.start();
System.out.println("C started");
tC.start();
}
虽然这是事实,但它仅指正在进行计算(或者在某些操作系统上,等待共享资源)的线程。在这种情况下,具有更高优先级的线程将获得更多的CPU时间,即将优先于竞争相同资源的线程执行
即使线程优先级会影响线程的启动顺序(很可能不会),所有线程实际上都可以在现代CPU上并行运行,因为它们不会相互影响
事实上,执行顺序完全由其他因素决定:线程不进行任何相关计算,它们将大部分(非常小的)执行时间用于等待共享资源,即System.out
必须查看代码才能发现System.out
(即PrintStream
的底层代码实际上执行以下操作:
因此,到达println()
的第一个线程会阻塞所有其他线程,直到写入其输出为止。第一个线程不考虑优先级,因为您无法中断同步块(这将破坏监视器的功能)
哪个线程首先获得锁不仅取决于线程优先级,甚至可能根本不取决于(Java)线程优先级。如果需要以精确的顺序执行线程,则不能使用线程优先级。您可以使用其中一个同步支持。(例如锁、信号灯) 你能发布你得到的输出吗?