5

Example:

class MyThread extends Thread{  
public MyThread(String name) {
    super(name);
}

public void run(){
    for (int i=0; i<5; i++) {
        System.out.println(Thread.currentThread().getName()
                +"("+Thread.currentThread().getPriority()+ ")"
                +", loop "+i);
    }
} 
}; 

public class Demo {  
    public static void main(String[] args) {  

    System.out.println(Thread.currentThread().getName()
            +"("+Thread.currentThread().getPriority()+ ")");

    Thread t1=new MyThread("t1");    // t1
    Thread t2=new MyThread("t2");    // t2
    t1.setPriority(1);                // t1 priority 1
    t2.setPriority(10);                //t2 priority 10
    t1.start();                        // start t1
    t2.start();                        // start t2
}  
}

When I execute the program, some times I have the output like below:

//Output1
main(5)
t2(10), loop 0
t2(10), loop 1
t2(10), loop 2
t2(10), loop 3
t2(10), loop 4
t1(1), loop 0
t1(1), loop 1
t1(1), loop 2
t1(1), loop 3
t1(1), loop 4

Sometimes I have output like below:

//Output2
main(5)
t1(1), loop 0
t1(1), loop 1
t1(1), loop 2
t1(1), loop 3
t1(1), loop 4
t2(10), loop 0
t2(10), loop 1
t2(10), loop 2
t2(10), loop 3
t2(10), loop 4 

In some other occasions I have output where t1 starts first, and t2 starts before t1 completes all output.

I thought output1 makes more sense as “Threads with higher priority are executed in preference to threads with lower priority.” How can we understand the reasoning behind this example?

1
  • 5
    You might profit from this
    – DSlomer64
    Aug 19, 2015 at 19:35

3 Answers 3

3

Your lower-prioritized thread is started first, therefore it may in some cases complete even before the higher-prioritized even starts. 5 iterations is not that much. On my (Windows) machine, if I replace the number of iterations with 100, the higher-prioritized thread is consistently selected first.

2

As you already mentioned in your post:

“Threads with higher priority are executed in preference to threads with lower priority.”

This does mean, that a thread with higher priority has a higher likeliness of being executed than a low-priority-thread. It doesn't mean that a thread with higher priority will always be executed first/finish first. The actual thread-handling depends upon the OS (java simply uses the thread-libraries provided by the OS it runs on).

3
  • for the downvoter: please explain why you downvoted the answer
    – user4668606
    Aug 20, 2015 at 11:28
  • I did not downvote you, but a possibility is that the answer is quite general and does not address OP's code
    – user140547
    Aug 20, 2015 at 19:21
  • @user140547 yeah, might be possible
    – user4668606
    Aug 20, 2015 at 19:39
0

My notes about threads priorities:

  • You can use it for improving performance
  • Priorities depends on OS (Windows has 7, Linux ignores them)
  • Don't design your app in way where correct working depends on priorities
  • If there are some threads with high priorities which are never deactivated, threads with lower priorities can never be run
1
  • If you're collecting rules of thumb, here's another: Threads that free up resources should run at a higher priority than threads that want to claim them. Aug 20, 2015 at 2:40

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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