3. Thread scheduling
Threads perform tasks, which have priority itself. Therefore, threads usually have a priority number associated to it. A thread is inactive when:
- In call a block operation,
wait()
,sleep()
- It is preempted, meaning the thread gives slot for another thread to run (a thread with higher priority, or because the current CPU cycles the current thread allowed to use is exceeded)
⚠️ Warning: Use priority only to affect scheduling policy for efficiency. Do not rely on thread priority for algorithm correctness. Attempting to write not thread-safe code by assuming the current thread can execute exclusively with higher priority, while there are other lower-prioritized threads access to that code, would potentially fail.
When coding a multithreaded application, consider following:
- Set each thread with a proper priority with
setPriority
onThread
object, for example, threads that respond to user input often has higher priority. - Normally, values for the thread priority of your app should be
NORM_PRIORITY
with a small delta around it. Be cautious to use constantMIN_PRIORITY
orMAX_PRIORITY
, because on some systems, this might affect other important programs.
Voluntary scheduling`
You can relinquish the current thread’s use of the CPU, by calling following Thread
static methods:
sleep(long millis)
: Sleep for millis of time.yield()
: signal the system that, the current thread need not run at the present time, give the chance for another thread to run. This call might be ignored by the system. Use this when you think it is better for other threads to run in your application, instead of this thread.