setpriority_Java Thread类的最终void setPriority(int priority)方法(带示例)

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.com/java/thread-class-final-void-setpriority-int-priority-method-with-example.aspx

setpriority

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值