java.lang.Thread.setPriority()%uA0方法改变该线程的优先级。
声明
以下是java.lang.Thread.setPriority()方法的声明
public final void setPriority(int newPriority)
参数
-
newPriority%uA0-- 这是优先考虑这个线程设置为这个值。
返回值
此方法不返回任何值。
异常
-
IllegalArgumentException%uA0-- 如果优先级不是在范围MIN_PRIORITY到MAX_PRIORITY。
-
SecurityException%uA0-- 如果当前线程不能修改该线程。
例子
下面的例子显示java.lang.Thread.setPriority()方法的使用。
package com.yiibai import java.lang.* public class ThreadDemo { public static void main(String[] args) { Thread t = Thread.currentThread() t.setName("Admin Thread") // set thread priority to 1 t.setPriority(1) // prints the current thread System.out.println("Thread = " + t) int count = Thread.activeCount() System.out.println("currently active threads = " + count) } }
让我们来编译和运行上面的程序,这将产生以下结果:
Thread = Thread[Admin Thread,1,main] currently active threads = 1