gpt4 book ai didi

java - thread.setPriority(0) 给出 IllegalArgumentException

转载 作者:行者123 更新时间:2023-11-29 09:44:00 26 4
gpt4 key购买 nike

当我运行这段代码时,它给了我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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com