Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Thread Priority in Java

The concept of priorities in threads states that each thread has a priority. In layman's terms, this means that each object has a priority, and the priority numbers range from 1 to 10.

  • The default setting for priority is 5.
  • Minimum priority is set to 1.
  • Maximum priority is set to 10.

How to get and set a thread's priority in Java.

  • public final int getPriority() : A thread's priority is given as an integer value by the getPriority() method. The thread priority is represent by a integer, between 1 to 10, where 1 means the lowest priority and 10 means the highest priority

    Thread thread = new Thread();
    int priority = thread.getPriority();
    System.out.println("Thread priority: " + priority);
    

  • public final void setPriority(int newPriority) : The setPriority() method is used to set the priority of the thread. you can pass an integer value between 1 and 10 as an argument. It will throw an IllegalArgumentException if you attempt to set a priority that is outside of this range, or from 1 to 10.
    Thread thread = new Thread();
    thread.setPriority(Thread.MAX_PRIORITY); // Sets the priority to the highest level (10)
    

  • The name of the current thread will be obtained using the currentThread() method. The setName() method can also be used by the user to give the threads names of their choosing for better comprehension.
  • The thread name will be obtained using the getName() method.





3 constants are defined Thread class

  • public static int NORM_PRIORITY
  • public static int MIN_PRIORITY
  • public static int MAX_PRIORITY

GetPriority() and setPriority() methods in a Java program are used to illustrate priorities in multithreading.


public class ThreadPriorityExample {
    public static void main(String[] args) {
        Thread thread1 = new Thread(new MyRunnable());
        Thread thread2 = new Thread(new MyRunnable());

        // Set the priorities of the threads
        thread1.setPriority(Thread.MIN_PRIORITY);
        thread2.setPriority(Thread.MAX_PRIORITY);

        // Start the threads
        thread1.start();
        thread2.start();

        // Print the priorities of the threads
        System.out.println("Thread 1 priority: " + thread1.getPriority());
        System.out.println("Thread 2 priority: " + thread2.getPriority());
    }

    static class MyRunnable implements Runnable {
        @Override
        public void run() {
            for (int i = 1; i <= 5; i++) {
                System.out.println("Running Thread: " + Thread.currentThread().getName() + " with priority " +
                        Thread.currentThread().getPriority());
            }
        }
    }
}




Output:

Running Thread: Thread-0 with priority 1
Running Thread: Thread-0 with priority 1
Running Thread: Thread-0 with priority 1
Running Thread: Thread-0 with priority 1
Running Thread: Thread-0 with priority 1
Running Thread: Thread-1 with priority 10
Running Thread: Thread-1 with priority 10
Running Thread: Thread-1 with priority 10
Running Thread: Thread-1 with priority 10
Running Thread: Thread-1 with priority 10
Thread 1 priority: 1
Thread 2 priority: 10
 

Example of thread have same priority in Java


public class Main extends Thread{ 
	public void run(){
        System.out.println("Run method");
    }
    public static void main(String[] args){	
        Thread.currentThread().setPriority(6);// The priority of the main thread is now set to 6.
        System.out.println("Priority of Main : "+ Thread.currentThread().getPriority());
        Main thread1 = new Main();
        // thread1 thread is child of main thread so thread1 thread will also have priority 6
        System.out.println("Priority of thread1 : "+ thread1.getPriority());
    }
}




Output:

Priority of Main thread : 6
Priority of thread1 : 6 
Daemon Thread in java Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class

Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.