Java Thread getPriority() Method

The Thread.getPriority() method in Java is used to retrieve the priority of a thread.

Table of Contents

  1. Introduction
  2. getPriority() Method Syntax
  3. Examples
    • Basic Usage
    • Setting and Getting Thread Priority
    • Using getPriority() in Multi-threaded Environment
  4. Real-World Use Case
  5. Conclusion

Introduction

The Thread.getPriority() method is a member of the Thread class that returns the priority of the thread. Thread priority is a hint to the thread scheduler about the importance of a thread relative to other threads. Higher-priority threads are more likely to be executed before lower-priority threads.

getPriority() Method Syntax

The syntax for the getPriority() method is as follows:

public final int getPriority()

Returns:

  • The priority of the thread. The returned value is an integer between Thread.MIN_PRIORITY (1) and Thread.MAX_PRIORITY (10).

Examples

Basic Usage

To demonstrate the basic usage of getPriority(), we will create a thread and retrieve its priority.

Example

public class GetPriorityExample {
    public static void main(String[] args) {
        Thread currentThread = Thread.currentThread();
        int priority = currentThread.getPriority();
        System.out.println("Current thread priority: " + priority);
    }
}

Output:

Current thread priority: 5

(Note: The default priority is typically Thread.NORM_PRIORITY, which is 5.)

Setting and Getting Thread Priority

You can set a thread's priority using the setPriority(int newPriority) method and then retrieve the priority using getPriority().

Example

public class SetAndGetPriorityExample {
    public static void main(String[] args) {
        Runnable task = () -> {
            Thread currentThread = Thread.currentThread();
            System.out.println("Thread name: " + currentThread.getName() + ", priority: " + currentThread.getPriority());
        };

        Thread thread = new Thread(task);
        thread.setName("CustomThread");
        thread.setPriority(Thread.MAX_PRIORITY); // Set the highest priority
        thread.start();
    }
}

Output:

Thread name: CustomThread, priority: 10

Using getPriority() in Multi-threaded Environment

In a multi-threaded environment, you can use the getPriority() method to get the priority of different threads.

Example

public class MultiThreadGetPriorityExample {
    public static void main(String[] args) {
        Runnable task = () -> {
            Thread currentThread = Thread.currentThread();
            System.out.println("Thread name: " + currentThread.getName() + ", priority: " + currentThread.getPriority());
        };

        Thread thread1 = new Thread(task, "Thread-1");
        Thread thread2 = new Thread(task, "Thread-2");

        thread1.setPriority(Thread.MIN_PRIORITY); // Set the lowest priority
        thread2.setPriority(Thread.MAX_PRIORITY); // Set the highest priority

        thread1.start();
        thread2.start();
    }
}

Output:

Thread name: Thread-1, priority: 1
Thread name: Thread-2, priority: 10

Real-World Use Case

Adjusting Thread Priorities for Task Management

In a real-world scenario, you might want to adjust thread priorities to manage tasks more effectively. For example, you can assign higher priority to critical tasks and lower priority to less important tasks.

Example

public class TaskManagementExample {
    public static void main(String[] args) {
        Runnable importantTask = () -> {
            Thread currentThread = Thread.currentThread();
            System.out.println("Important task running in thread: " + currentThread.getName() + ", priority: " + currentThread.getPriority());
        };

        Runnable regularTask = () -> {
            Thread currentThread = Thread.currentThread();
            System.out.println("Regular task running in thread: " + currentThread.getName() + ", priority: " + currentThread.getPriority());
        };

        Thread importantThread = new Thread(importantTask, "ImportantThread");
        Thread regularThread = new Thread(regularTask, "RegularThread");

        importantThread.setPriority(Thread.MAX_PRIORITY);
        regularThread.setPriority(Thread.NORM_PRIORITY);

        importantThread.start();
        regularThread.start();
    }
}

Output:

Important task running in thread: ImportantThread, priority: 10
Regular task running in thread: RegularThread, priority: 5

Conclusion

The Thread.getPriority() method in Java provides a way to retrieve the priority of a thread. By understanding how to use this method, you can manage and optimize the execution of threads in your Java applications. Whether you are working with single-threaded or multi-threaded environments, the getPriority() method offers used for interacting with thread priorities and ensuring that critical tasks are executed with the appropriate level of importance.

Comments