任务的大小太小,可能会在开始后立即完成。此外,如果您有“足够”的 CPU 内核,每个工作线程将分配给一个内核,因此结果将是相同的。
在不同的上下文中尝试实验,首先增加任务大小(例如通过循环一千次到一百万次,不打印)然后增加线程数以超过您拥有的内核数,第三,首先创建您的线程并然后启动所有线程(你不能一次启动它们,你仍然需要循环它们)。
就我而言,我选择了 10 个线程,因为我在具有两个超线程内核的处理器上运行代码,同时运行四个线程。
我对您的示例的更改:
public class ThreadPriority implements Runnable {
public synchronized void run() {
System.out.println("Starting Implementation of Thread " + Thread.currentThread().getName());
float s = 0;
for (int i = 0; i < 1000; i++)
for (int k = 0; k < 1000000; k++)
s += k;
System.out.println("Ending Implementation of Thread " + Thread.currentThread().getName() + " " + s);
}
Thread t;
public ThreadPriority(String name, int prio) {
t = new Thread(this);
t.setName(name);
t.setPriority(prio);
}
public void start() {
synchronized (t) {
t.start();
}
}
public static void main(String[] args) {
System.out.println("Program starts...");
ThreadPriority[] th = new ThreadPriority[10];
for (int i = 0; i < th.length; i++) {
th[i] = new ThreadPriority("T" + i, i / 2 + 1);
}
for (ThreadPriority tp : th)
tp.start();
System.out.println("Program ending, wait for all the threads to complete");
}
}
结果是:
Program starts...
Starting Implementation of Thread T0
Starting Implementation of Thread T9
Starting Implementation of Thread T8
Starting Implementation of Thread T5
Program ending, wait for all the threads to complete
Starting Implementation of Thread T4
Starting Implementation of Thread T6
Starting Implementation of Thread T7
Starting Implementation of Thread T2
Starting Implementation of Thread T3
Starting Implementation of Thread T1
Ending Implementation of Thread T6 1.7592186E13
Ending Implementation of Thread T7 1.7592186E13
Ending Implementation of Thread T4 1.7592186E13
Ending Implementation of Thread T8 1.7592186E13
Ending Implementation of Thread T9 1.7592186E13
Ending Implementation of Thread T5 1.7592186E13
Ending Implementation of Thread T2 1.7592186E13
Ending Implementation of Thread T0 1.7592186E13
Ending Implementation of Thread T1 1.7592186E13
Ending Implementation of Thread T3 1.7592186E13
正如你所看到的,低数量的线程往往会更晚结束,因为高数量的线程具有更高的优先级。通过将天平倒置:
for (int i = 0; i < th.length; i++) {
th[i] = new ThreadPriority("T" + i, 9 - i / 2 );
}
低数量的线程比高数量的线程完成得更快。一些线程甚至在其他线程启动之前就完成了,因为它们与调用程序相比具有更高的优先级:
Program starts...
Starting Implementation of Thread T0
Starting Implementation of Thread T1
Starting Implementation of Thread T2
Starting Implementation of Thread T3
Program ending, wait for all the threads to complete
Ending Implementation of Thread T2 1.7592186E13
Ending Implementation of Thread T3 1.7592186E13
Ending Implementation of Thread T0 1.7592186E13
Ending Implementation of Thread T1 1.7592186E13
Starting Implementation of Thread T9
Starting Implementation of Thread T4
Starting Implementation of Thread T8
Starting Implementation of Thread T7
Starting Implementation of Thread T5
Starting Implementation of Thread T6
Ending Implementation of Thread T4 1.7592186E13
Ending Implementation of Thread T5 1.7592186E13
Ending Implementation of Thread T7 1.7592186E13
Ending Implementation of Thread T8 1.7592186E13
Ending Implementation of Thread T9 1.7592186E13
Ending Implementation of Thread T6 1.7592186E13