The concept of priorities in threads states that each thread has a priority. In layman’s terms, each object has a priority here, and the priorities are represented by numbers from 1 to 10.
Note:
// Java Program to Illustrate Priorities in Multithreading
// via help of getPriority() and setPriority() method
// Importing required classes
import java.lang.*;
// Main class
public class Coderz extends Thread {
// Method 1
// run() method for the thread that is called
// as soon as start() is invoked for thread in main()
public void run()
{
// Print statement
System.out.println(" run() method");
}
// Main driver method
public static void main(String[] args)
{
// Creating random threads
// with the help of above class
Coderz t1 = new Coderz();
Coderz t2 = new Coderz();
Coderz t3 = new Coderz();
// Thread 1
// Display the priority of above thread
// using getPriority() method
System.out.println("t1 thread priority : "
+ t1.getPriority());
// Thread 1
// Display the priority of above thread
System.out.println("t2 thread priority : "
+ t2.getPriority());
// Thread 3
System.out.println("t3 thread priority : "
+ t3.getPriority());
// Setting priorities of above threads by
// passing integer arguments
t1.setPriority(8);
t2.setPriority(1);
t3.setPriority(3);
// t3.setPriority(21); will throw
// IllegalArgumentException
// 8
System.out.println("t1 thread priority : "
+ t1.getPriority());
// 1
System.out.println("t2 thread priority : "
+ t2.getPriority());
// 3
System.out.println("t3 thread priority : "
+ t3.getPriority());
// Main thread
// Displays the name of
// currently executing Thread
System.out.println(
"Currently Executing Thread : "
+ Thread.currentThread().getName());
System.out.println(
"Main thread priority : "
+ Thread.currentThread().getPriority());
// Main thread priority is set to 10
Thread.currentThread().setPriority(10);
System.out.println(
"Main thread priority : "
+ Thread.currentThread().getPriority());
}
}
t1 thread priority : 5
t2 thread priority : 5
t3 thread priority : 5
t1 thread priority : 8
t2 thread priority : 1
t3 thread priority : 3
Currently Executing Thread : main
Main thread priority : 5
Main thread priority : 10
import java.lang.*;
public class IllegalArgumentException extends Thread
{
// the main method
public static void main(String argvs[])
{
// Now, priority of the main thread is set to 11, which is greater than 10
Thread.currentThread().setPriority(11);
// The current thread is retrieved
// using the currentThread() method
// displaying the main thread priority
// using the getPriority() method of the Thread class
System.out.println("Priority of the main thread is : " + Thread.currentThread().getPriority());
}
}
Exception in thread "main" java.lang.IllegalArgumentException
at java.base/java.lang.Thread.setPriority(Thread.java:1138)
at IllegalArgumentException.main(IllegalArgumentException.java:11)
Note: also read about the Naming a Thread in Java
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…