HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
1. Feel Free to ask and submit anything on and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.

corejava-multithreading: Threads priority in Java

If we have 10 threads waiting in queue with the same priority, which thread will get executed first and how ? ,please explain with an example.

corejava x 353
multithreading x 21
Posted On : 2014-12-24 15:25:59.0
profile Adarsh S - Adarsh S


we can´t decide which thread will get executed first. Threads having higher priority doesn´t mean that it will get executed first. Thread scheduler uses thread priorities to decide when a thread should be allowed to run. Higher-priority threads get more CPU time than lower-priority threads. In practice, the amount of CPU time that a thread gets often depends on several factors besides its priority. (For example, how an operating system implements multitasking can affect the relative availability of CPU time.)

A higher-priority thread can also preempt a lower-priority one. For instance, when a lower-priority thread is running and a higher-priority thread resumes (from sleeping or waiting on I/O, for example), it will preempt the lower-priority thread.

In theory, threads of equal priority should get equal access to the CPU. But you should be careful. and keep in mind, Java is designed to work in a wide range of environments. Some of those environments implement multitasking fundamentally differently than others. For safety, threads that share the same priority should yield control once in a while. This ensures that all threads have a chance to run under a non preemptive operating system.

Practically, even in non preemptive environments, mostly threads get a chance to run, because mostly threads inevitably encounter some blocking situation, such as waiting for I/O. When it happens, the blocked thread is suspended and other threads can run. But, if you want smooth multithreaded execution, you are better off not relying on this. Also, some types of tasks are CPU-intensive. Such threads dominate the CPU. For these types of threads, you want to yield control occasionally, so that other threads can run.

Posted On : 2014-12-24 17:07:32
Satisfied : 2 Yes  0 No
profile Garima Gupta - Garima Gupta
Reply This Thread

Post Answer
Please Login First to Post Answer: Login login with facebook -