Junior — Middle
How can you set a priority for a thread of execution?
sobes.tech AI
Answer from AI
In Java, the thread priority can be set using the setPriority(int priority) method of the Thread class. Priorities are set with numbers from 1 (minimum) to 10 (maximum), with the default being 5 (Thread.NORM_PRIORITY).
Example:
Thread thread = new Thread(() -> {
// thread code
});
thread.setPriority(Thread.MAX_PRIORITY); // maximum priority
thread.start();
It is important to understand that priorities are only hints to the operating system's thread scheduler and do not guarantee strict execution order.