-4

Each time when i run the program the order of execution changes due to max priority assign o thread b it should be started first and completed first the output is not accordingly

threadA.setPriority(Thread.MIN_PRIORITY);
threadB.setPriority(Thread.MAX_PRIORITY)
System.out.println("Start Thread A");
threadA.start();

System.out.println("Start Thread B");
threadB.start();

System.out.println("End of main Thread");
LaurentG
  • 11,128
  • 9
  • 51
  • 66

3 Answers3

3

I'd like to quote xagyg's answer:

Let's keep it simple and go straight to the source ...

Every thread has a priority. When there is competition for processing resources, threads with higher priority are generally executed in preference to threads with lower priority. Such preference is not, however, a guarantee that the highest priority thread will always be running, and thread priorities cannot be used to reliably implement mutual exclusion.

from the Java Language Specification (2nd Edition) p.445. Also ...

Although thread priorities exist in Java and many references state that the JVM will always select one of the highest priority threads for scheduling [52, 56, 89], this is currently not guaranteed by the Java language or virtual machine specifications [53, 90]. Priorities are only hints to the scheduler [127, page 227].

from Testing Concurrent Java Components (PhD Thesis, 2005) p. 62.

Reference 127, page 227 (from the excerpt above) is from Component Software: Beyond Object-Oriented Programming (by C. Szyperski), Addison Wesley, 1998.

In short, do not rely on thread priorities.

link to the post

Community
  • 1
  • 1
sataniccrow
  • 372
  • 2
  • 7
1

Thread priority is a hint to the OS and only matters if you use very high e.g. 100% cpu utilisation on all your CPU. A thread can run to completion in the time it takes another thread to start.

BTW: You have to be administrator on Windows and root or have setcap permissions on Linux to raise priority.

In short, changing the priority is next to useless, and cannot be relied upon.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Are you sure you need to be root/admnistrator? I mean, I know you need to if you want to increase a process priority among other processes, but I always though you wouldn't need when increasing priority of a thread among other threads of the same process. – Étienne Miret Oct 02 '13 at 08:03
  • @EtienneMiret When you raise the priority of a thread, it is raises against all threads. Under Linux I think you need permissions to change priority at all, but I am not sure where I read that. – Peter Lawrey Oct 02 '13 at 08:13
0

If you have a machine with two or more cores running two threads (each assigned to its own core) - which is the usual situation - then thread priority has likely now effect. Every thread will run at maximum single core speed.

Also: You start thread A from your main thread first. Then you start thread B. How fast is you main thread? Is thread A completed faster than starting a thread - thread B?

If you like to ensure order of execution you have to use join, not a priority.

See also Java Thread priority has no effect

Community
  • 1
  • 1
Christian Fries
  • 16,175
  • 10
  • 56
  • 67