Java Thread Priority Example

Introduction

In Java, each thread is assigned a priority that helps the thread scheduler decide the order in which threads are executed. Thread priority is an integer value that ranges from Thread.MIN_PRIORITY (1) to Thread.MAX_PRIORITY (10). The default priority is Thread.NORM_PRIORITY (5). The thread scheduler uses these priorities to decide when each thread should run. However, thread priority does not guarantee the order of execution; it is merely a suggestion to the thread scheduler.

Table of Contents

  1. Setting Thread Priority
  2. Getting Thread Priority
  3. Example: Setting and Getting Thread Priority
  4. Conclusion

1. Setting Thread Priority

You can set a thread's priority using the setPriority(int newPriority) method of the Thread class. The new priority must be within the range of Thread.MIN_PRIORITY and Thread.MAX_PRIORITY.

Syntax:

public final void setPriority(int newPriority)

2. Getting Thread Priority

You can get a thread's priority using the getPriority() method of the Thread class. This method returns the priority of the thread as an integer.

Syntax:

public final int getPriority()

3. Example: Setting and Getting Thread Priority

Let's create an example to demonstrate how to set and get the priority of a thread.

Example:

class MyThread extends Thread {
    public MyThread(String name) {
        super(name); // Call the parent constructor to set the thread name
    }

    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            System.out.println(Thread.currentThread().getName() + " with priority " +
                               Thread.currentThread().getPriority() + " is running.");
        }
    }

    public static void main(String[] args) {
        MyThread thread1 = new MyThread("Thread-1");
        MyThread thread2 = new MyThread("Thread-2");
        MyThread thread3 = new MyThread("Thread-3");

        // Set the priorities
        thread1.setPriority(Thread.MIN_PRIORITY); // Priority 1
        thread2.setPriority(Thread.NORM_PRIORITY); // Priority 5
        thread3.setPriority(Thread.MAX_PRIORITY); // Priority 10

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

        // Display the priorities
        System.out.println(thread1.getName() + " priority: " + thread1.getPriority());
        System.out.println(thread2.getName() + " priority: " + thread2.getPriority());
        System.out.println(thread3.getName() + " priority: " + thread3.getPriority());
    }
}

Output:

Thread-1 priority: 1
Thread-2 priority: 5
Thread-3 priority: 10
Thread-3 with priority 10 is running.
Thread-3 with priority 10 is running.
Thread-3 with priority 10 is running.
Thread-2 with priority 5 is running.
Thread-2 with priority 5 is running.
Thread-2 with priority 5 is running.
Thread-1 with priority 1 is running.
Thread-1 with priority 1 is running.
Thread-1 with priority 1 is running.

Explanation:

  • The MyThread class extends the Thread class and calls the parent constructor to set the thread name.
  • The run method prints the name and priority of the current thread.
  • In the main method, three MyThread objects are created with custom names.
  • The priorities of the threads are set using the setPriority method.
  • The threads are started using the start method.
  • The priorities of the threads are printed using the getPriority method.

4. Conclusion

Thread priority in Java is a useful feature that helps the thread scheduler decide the order in which threads should run. By setting and getting thread priorities, you can influence the execution of threads in your programs. However, keep in mind that thread priority is only a suggestion to the thread scheduler and does not guarantee the order of execution. This guide provided an example of how to set and get thread priorities in Java.

Happy coding!

Comments

  1. http://www.javaguides.net/p/java-multithreading-utorial.html

    ReplyDelete

Post a Comment

Leave Comment