admin管理员组

文章数量:1623784

setpriority

线程类最终void setPriority(int priority) (Thread Class final void setPriority(int priority))

  • This method is available in package java.lang.Thread.setPriority(int priority).

    软件包java.lang.Thread.setPriority(int priority)中提供了此方法。

  • This method is used to set the priority of this thread.

    此方法用于设置此线程的优先级。

  • This method is not static so this method is accessible with Thread class object it is not accessible with the class name.

    此方法不是静态的,因此该方法可通过Thread类对象访问,而无法通过类名称访问。

  • This method is final so we can't override this method in our program.

    此方法是最终方法,因此我们无法在程序中覆盖此方法。

  • The return type of this method is void so it does not return anything.

    此方法的返回类型为void,因此它不返回任何内容。

  • This method does not raise any exception.

    此方法不会引发任何异常。

  • We need to notice that if we don't assign any priority explicitly the default priority of this thread is 5.

    我们需要注意的是,如果我们未明确分配任何优先级,则此线程的默认优先级为5。

  • Priority range will lie between 1 and 10 and 1 is the min_priority and 10 is the max_priority of a thread.

    优先级范围在1到10之间,其中1是线程的min_priority ,而10是max_priority 。

Syntax:

句法:

    final void setPriority(int priority){
    }

Parameter(s):

参数:

We pass only one object as a parameter in the method of the Thread and the parameter is the priority of this thread too.

我们仅在Thread方法中传递一个对象作为参数,该参数也是该线程的优先级。

Return value:

返回值:

The return type of this method is void, it does not return anything.

此方法的返回类型为void ,它不返回任何内容。

Java程序演示setPriority()方法的示例 (Java program to demonstrate example of setPriority() method)

/*  We will use Thread class methods so we are importing 
    the package but it is not mandate because 
    it is imported by default
*/

import java.lang.Thread;

class SetThreadPriority extends Thread {
    // Override run() of Thread class
    public void run() {
        System.out.println("Thread Name : " + Thread.currentThread().getName());
        System.out.println("Current thread priority is : " + Thread.currentThread().getPriority());
        // By using setPriority() method is used to change 
        // the priority of this thread
        Thread.currentThread().setPriority(6);
        System.out.println("New thread priority is : " + Thread.currentThread().getPriority());
    }

    public static void main(String[] args) {
        // Creating an object of SetThreadPriority class
        SetThreadPriority st_priority = new SetThreadPriority();

        // We are setting the name of the thread GetThreadPriority
        st_priority.setName("SetThreadPriority");

        // Calling start() method with SetThreadPriority class 
        // object of Thread class
        st_priority.start();
    }
}

Output

输出量

E:\Programs>javac SetThreadPriority.java

E:\Programs>java SetThreadPriority
Thread Name : SetThreadPriority
Current thread priority is : 5
New thread priority is : 6


翻译自: https://www.includehelp/java/thread-class-final-void-setpriority-int-priority-method-with-example.aspx

setpriority

本文标签: 示例方法voidthreadsetpriorityJava