I do not understand the output of this code:
package examplepriorities;
class Counter extends Thread {
public Counter(String name) {
super(name);
}
@Override
public void run() {
int count = 0;
while (count <= 1000) {
System.out.println(this.getName() + ": " + count++);
}
}
}
public class ExamplePriorities {
public static void main(String[] args) {
Counter thread1 = new Counter("thread 1");
thread1.setPriority(10);
Counter thread2 = new Counter("thread 2");
thread2.setPriority(1);
thread1.start();
thread2.start();
}
}
At the output, you can see messages printed from thread 1 from 0 to 1000, and when these threads finish, the second thread starts to print messages. I know that the first thread has a higher priority level, but since there are (I suppose) free cores in the processor, why don't both threads do their work at the same time?
source
share