抛出的异常表明向方法传递了一个不合法或不正确的参数。
代码演示
public class ThreadPriorityDemo { public static void main(String[] args) { ThreadPriority tp1 = new ThreadPriority(); ThreadPriority tp2 = new ThreadPriority(); ThreadPriority tp3 = new ThreadPriority(); tp1.setName("奥拉夫"); tp2.setName("挖掘机"); tp3.setName("德邦总管"); //获取默认优先级 //System.out.println(tp1.getPriority());//5 //System.out.println(tp2.getPriority());//5 //System.out.println(tp3.getPriority());//5 //设置优先值 //IllegalArgumentException:非法参数异常 tp1.setPriority(1000); //通过查看API设置正确的优先级 // tp1.setPriority(1); // tp3.setPriority(10); //启动线程 // tp1.start(); // tp2.start(); // tp3.start(); }}结果:
Exception in thread "main" java.lang.IllegalArgumentExceptionat java.base/java.lang.Thread.setPriority(Thread.java:1188)at com.ginger.demo03.ThreadPriorityDemo.main(ThreadPriorityDemo.java:21)