Explanation
1. Setting the priority is only to a large extent to allow a thread to get as many execution opportunities as possible, that is, to allow the thread itself to be scheduled by the operating system as much as possible , instead of setting a high priority, it will run first, or a thread with a high priority will run first than a thread with a low priority.
2. To set the priority, just call setPriority() directly.
Example
public final void setPriority(int newPriority) { this.checkAccess(); if (newPriority <= 10 && newPriority >= 1) { ThreadGroup g; if ((g = this.getThreadGroup()) != null) { if (newPriority > g.getMaxPriority()) { newPriority = g.getMaxPriority(); } this.setPriority0(this.priority = newPriority); } } else { throw new IllegalArgumentException(); } }
The above is the detailed content of How to use setPriority() method to set thread priority in Java?. For more information, please follow other related articles on the PHP Chinese website!