Home > Java > javaTutorial > How to use setPriority() method to set thread priority in Java?

How to use setPriority() method to set thread priority in Java?

PHPz
Release: 2023-04-21 20:28:06
forward
1497 people have browsed it

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();
    }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template