Java Thread setPriority() methodThe setPriority() method of thread class is used to change the thread's priority. Every thread has a priority which is represented by the integer number between 1 to 10. Thread class provides 3 constant properties:
We can also set the priority of thread between 1 to 10. This priority is known as custom priority or user defined priority. SyntaxParametera: It is the priority to set this thread to. ReturnIt does not return any value. ExceptionIllegalArgumentException: This exception throws if the priority is not in the range MIN_PRIORITY to MAX_PRIORITY. SecurityException: This exception throws if the current thread cannot modify this thread. Example 1: Maximum Priority ThreadTest it NowOutput: Priority of thread is: 10 Example 2: Minimum Priority ThreadTest it NowOutput: Priority of thread is: 1 Example 3: Normal Priority ThreadTest it NowOutput: Priority of thread is: 5 Example 4: User define Priority ThreadTest it NowOutput: Priority of thread t1 is: 4 Priority of thread t2 is: 7 running... Example 5: When priority is greater than 10Test it NowOutput: Exception in thread "main" java.lang.IllegalArgumentException at java.lang.Thread.setPriority(Thread.java:1089) at JavaSetPriorityExp5.main(JavaSetPriorityExp5.java:13) Next TopicMultithreading in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India