java - thread.setPriority(0) 给出 IllegalArgumentException

标签 java multithreading concurrency illegalargumentexception

当我运行这段代码时,它给了我IllegalArgumentException,然后执行了整个代码,但是线程 t 的名称只是默认值,而不是代码中的 Mark。

可能是什么原因?

    Exception in thread "main" java.lang.IllegalArgumentException
        at java.lang.Thread.setPriority(Unknown Source)
        at Threads.CurrentThreadImpl.main(CurrentThreadImpl.java:11)
    value of I is : 0and the thread name is : Thread-0
    value of I is : 1and the thread name is : Thread-0
    value of I is : 2and the thread name is : Thread-0
    value of I is : 3and the thread name is : Thread-0
    value of I is : 0and the thread name is : Thread-1
    value of I is : 1and the thread name is : Thread-1
    value of I is : 2and the thread name is : Thread-1
    value of I is : 3and the thread name is : Thread-1

    public class CreateThread implements Runnable{

    public void run(){
        for(int i = 0; i<4; i++){
            System.out.println("value of I is : "+ i + "and the thread name is : "+ Thread.currentThread().getName());
        }
    }
}

    public class CurrentThreadImpl {

    public static void main(String[] args) {
        CreateThread runnableObj = new CreateThread();
        Thread thread = new Thread(runnableObj);
        Thread t = new Thread(runnableObj);
        thread.start();
        t.start();
        thread.setPriority(0);
        t.setPriority(10);
        t.setName("Mark");
    }

}

最佳答案

参见 Thread#setPriority :

IllegalArgumentException - If the priority is not in the range MIN_PRIORITY to MAX_PRIORITY.

MIN_PRIORITY是 1,不是 0:

enter image description here

关于java - thread.setPriority(0) 给出 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18944126/

相关文章:

java - Hadoop 设置最大并发 mapreduce 任务在伪模式下不起作用

multithreading - LWJGL、Clojure、OpenGL 命令的单线程

Java内存泄漏示例

java - 奥林戈 (OData 4) : how set translation of EntitySet (Entity) names?

objective-c - 为什么我的 Cocoa 应用程序中的某些行需要永远运行?

java - 并发 hashmap 和 copyonwritearraylist

Java 内存模型 : volatiles and reads/writes reordering

mysql - Passenger Nginx 如何处理并发请求

java - 为什么我们不能对声明为父类类型且元素包含子类引用的数组使用仅子方法?

java - Android 使用证书建立 HTTPSConnection