I don't understand the output of this code:

package examplepriorities;class Counter extends Thread {public Counter(String name) {super(name);}@Overridepublic 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();}}

In the output you can see messages printed from the thread 1 from 0 to 1000 and, when these thread finish its work, the second thread start printing messages.I know that the first thread has higher priority level but as there are (i suppose) free cores in the processor, why both threads don't make their job at the same time?

  • 1
    It's because you start the second after the first. There is nothing to tell the thread 2 to execute at the same time/ in parallel. Check ExecutorService– Xavier BoucletNov 9 '17 at 15:42
  • possibly because println is synchronized– assyliasNov 9 '17 at 15:46
  • @XavierBouclet I'm not sure you're right. Threads should run in parallel.– gidimNov 9 '17 at 15:47
  • @assylias but that's for a single call isn't it? both threads can call println() one after another.– gidimNov 9 '17 at 15:48
  • Maybe there is not enough work and so the highest priority thread can do all his part, try inserting some sleep– Joachim HuetNov 9 '17 at 15:50

This seems to be the way your OS scheduler decided to schedule them. If I run your code I get:

thread 1: 0thread 1: 1thread 1: 2thread 1: 3thread 1: 4thread 1: 5thread 1: 6thread 1: 7thread 1: 8thread 1: 9thread 1: 10thread 1: 11thread 1: 12thread 1: 13thread 1: 14thread 1: 15thread 1: 16thread 1: 17thread 1: 18thread 1: 19thread 1: 20thread 1: 21thread 1: 22thread 1: 23thread 1: 24thread 1: 25thread 1: 26thread 1: 27thread 1: 28thread 1: 29thread 1: 30thread 1: 31thread 1: 32thread 1: 33thread 1: 34thread 1: 35thread 1: 36thread 1: 37thread 1: 38thread 1: 39thread 2: 0thread 2: 1thread 2: 2thread 2: 3thread 2: 4thread 1: 40thread 1: 41thread 1: 42thread 1: 43thread 1: 44thread 2: 5thread 2: 6thread 2: 7thread 2: 8thread 2: 9thread 2: 10thread 1: 45thread 1: 46thread 1: 47thread 1: 48thread 2: 11thread 1: 49thread 1: 50thread 1: 51thread 1: 52thread 1: 53thread 2: 12thread 2: 13thread 2: 14thread 2: 15thread 2: 16thread 2: 17thread 2: 18thread 1: 54thread 2: 19thread 2: 20thread 2: 21thread 2: 22thread 1: 55thread 1: 56thread 2: 23thread 1: 57thread 2: 24thread 2: 25thread 2: 26thread 1: 58thread 1: 59thread 1: 60thread 2: 27thread 2: 28thread 2: 29thread 1: 61thread 2: 30thread 1: 62thread 2: 31thread 1: 63

    If you increase amount of work each thread should do, by setting the number of iterations to 100_000000 for example, threads will execute in parallel.

    thread 1: 1447678thread 2: 127862thread 2: 127863thread 2: 127864thread 2: 127865thread 2: 127866

    From the output it is obvious that thread two is scheduled before thread one has finished.

    Min priority thread is simply competing with threads from other applications/OS for the CPU resources, and since it has lowest possible priority it doesn't get scheduled before 1000 iterations are executed.

    It's working as expected, second thread has lowest possible priority and therefore gets appropriately small amount of CPU time.

    • If I execute the application in a environment with an only one processor, the output is similar. In fact, I can see some kind of alternation but I don´t understand the reason so one thread has upper priority.– Alberto Nov 10 '17 at 10:17
    • Well thread one gets more execution time because it has higher priority than thread 2. Priority 10 is a hint for time critical execution, while 1 means lowest possible priority. Therefore CPU will schedule thread one to do it's work most of the time. In practice you should not touch these priorities, behavior is much too dependent on the architecture of hw and OS.– JohnNov 10 '17 at 10:40

    Your Answer

     
    discard

    By posting your answer, you agree to the privacy policy and terms of service.

    Not the answer you're looking for? Browse other questions tagged or ask your own question.