Difference between revisions of "JAVA: Thread Priority di Multithreading"

From OnnoWiki
Jump to navigation Jump to search
(Created page with "As we already know java being completely object-oriented works within a multithreading environment in which thread scheduler assigns the processor to a thread based on the pri...")
 
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
As we already know java being completely object-oriented works within a multithreading environment in which thread scheduler assigns the processor to a thread based on the priority of thread. Whenever we create a thread in Java, it always has some priority assigned to it. Priority can either be given by JVM while creating the thread or it can be given by the programmer explicitly.  
+
Seperti yang telah kita ketahui, Java sepenuhnya object-oriented bekerja dalam lingkungan multithreading di mana penjadwal thread menetapkan prosesor ke thread berdasarkan prioritas thread . Setiap kali kita membuat thread di Java, selalu ada prioritas yang ditetapkan padanya. Prioritas dapat diberikan oleh JVM saat membuat thread atau dapat diberikan oleh programmer secara eksplisit.
  
Priorities in threads is a concept where each thread is having a priority which in layman’s language one can say every object is having priority here which is represented by numbers ranging from 1 to 10.  
+
Prioritas dalam thread adalah sebuah konsep di mana setiap thread memiliki prioritas yang dalam bahasa awam bisa dikatakan setiap object memiliki prioritas di sini yang diwakili oleh angka mulai dari 1 hingga 10.
 +
* Default priority diatur ke 5 sebagai pengecualian.
 +
* Minimum priority diatur ke 1.
 +
* Maximum priority diatur ke 10.
  
The default priority is set to 5 as excepted.
+
Berikut 3 konstanta yang didefinisikan di dalamnya yaitu sebagai berikut:
Minimum priority is set to 1.
+
* public static int NORM_PRIORITY
Maximum priority is set to 10.
+
* public static int MIN_PRIORITY
Here 3 constants are defined in it namely as follows:
+
* public static int MAX_PRIORITY
  
public static int NORM_PRIORITY
+
Mari kita diskusikan dengan sebuah contoh untuk mengetahui bagaimana pekerjaan itu dijalankan secara internal. Di sini kita akan menggunakan pengetahuan yang dikumpulkan di atas sebagai berikut:
public static int MIN_PRIORITY
+
* Kita akan menggunakan method currentThread() untuk mendapatkan nama thread saat ini. Pengguna juga dapat menggunakan method setName() jika dia ingin membuat nama thread sesuai pilihan untuk tujuan pemahaman.
public static int MAX_PRIORITY
+
* method getName() akan digunakan untuk mendapatkan nama thread.
Let us discuss it with an example to get how internally the work is getting executed. Here we will be using the knowledge gathered above as follows:
+
Nilai prioritas yang diterima untuk thread berada dalam kisaran 1 hingga 10.
  
We will use currentThread() method to get the name of the current thread. User can also use setName() method if he/she wants to make names of thread as per choice for understanding purposes.
+
Mari kita bahas cara mendapatkan dan menetapkan prioritas thread di java.
getName() method will be used to get the name of the thread.
+
* public final int getPriority(): java.lang.Thread.getPriority() method mengembalikan prioritas dari thread yang diberikan.
The accepted value of priority for a thread is in the range of 1 to 10.  
+
* public final void setPriority(int newPriority): java.lang.Thread.setPriority() method mengubah prioritas thread menjadi nilai newPriority. Method ini melempar IllegalArgumentException jika nilai parameter newPriority melampaui batas minimum(1) dan maksimum(10).
  
Let us do discuss how to get and set priority of a thread in java.
+
Contoh
  
public final int getPriority(): java.lang.Thread.getPriority() method returns priority of given thread.
+
// Java Program to Illustrate Priorities in Multithreading
public final void setPriority(int newPriority): java.lang.Thread.setPriority() method changes the priority of thread to the value newPriority. This method throws IllegalArgumentException if value of parameter newPriority goes beyond minimum(1) and maximum(10) limit.
+
// via help of getPriority() and setPriority() method
Example
+
 
 +
// Importing required classes
 +
import java.lang.*;
 +
 
 +
// Main class
 +
class ThreadDemo 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("Inside run method");
 +
    }
 +
 
 +
    // Main driver method
 +
    public static void main(String[] args)
 +
    {
 +
        // Creating random threads
 +
        // with the help of above class
 +
        ThreadDemo t1 = new ThreadDemo();
 +
        ThreadDemo t2 = new ThreadDemo();
 +
        ThreadDemo t3 = new ThreadDemo();
 +
 
 +
        // 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(2);
 +
        t2.setPriority(5);
 +
        t3.setPriority(8);
 +
 
 +
        // t3.setPriority(21); will throw
 +
        // IllegalArgumentException
 +
 
 +
        // 2
 +
        System.out.println("t1 thread priority : "
 +
                            + t1.getPriority());
 +
 
 +
        // 5
 +
        System.out.println("t2 thread priority : "
 +
                            + t2.getPriority());
 +
 
 +
        // 8
 +
        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());
 +
    }
 +
}
  
 +
Output
 +
t1 thread priority : 5
 +
t2 thread priority : 5
 +
t3 thread priority : 5
 +
t1 thread priority : 2
 +
t2 thread priority : 5
 +
t3 thread priority : 8
 +
Currently Executing Thread : main
 +
Main thread priority : 5
 +
Main thread priority : 10
  
// Java Program to Illustrate Priorities in Multithreading
+
Penjelasan Output:
// via help of getPriority() and setPriority() method
+
* Thread dengan prioritas tertinggi akan mendapatkan kesempatan eksekusi sebelum thread lainnya. Misalkan ada 3 thread t1, t2, dan t3 dengan prioritas 4, 6, dan 1. Jadi, thread t2 akan dieksekusi terlebih dahulu berdasarkan prioritas maksimum 6 setelah itu t1 akan dieksekusi dan kemudian t3.
+
* Prioritas default untuk Thread utama selalu 5, dapat diubah nanti. Prioritas default untuk semua Thread lainnya bergantung pada prioritas Thread induk.
// Importing required classes
+
 
import java.lang.*;
+
Kita biasanya bertanya-tanya bagaimana cara kita menetapkan prioritas yang sama untuk thread daripada apa yang akan terjadi. Semua pemrosesan untuk menjaga thread dilakukan dengan bantuan penjadwal thread. Kita dapat merujuk pada contoh di bawah ini tentang apa yang akan terjadi jika prioritas ditetapkan sama dan selanjutnya kita akan membahasnya sebagai penjelasan keluaran untuk memiliki pemahaman yang lebih baik secara konseptual dan praktis.
 
// Main class
 
class ThreadDemo 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("Inside run method");
 
    }
 
 
    // Main driver method
 
    public static void main(String[] args)
 
    {
 
        // Creating random threads
 
        // with the help of above class
 
        ThreadDemo t1 = new ThreadDemo();
 
        ThreadDemo t2 = new ThreadDemo();
 
        ThreadDemo t3 = new ThreadDemo();
 
 
        // 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(2);
 
        t2.setPriority(5);
 
        t3.setPriority(8);
 
 
        // t3.setPriority(21); will throw
 
        // IllegalArgumentException
 
 
        // 2
 
        System.out.println("t1 thread priority : "
 
                          + t1.getPriority());
 
 
        // 5
 
        System.out.println("t2 thread priority : "
 
                          + t2.getPriority());
 
 
        // 8
 
        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());
 
    }
 
}
 
Output
 
t1 thread priority : 5
 
t2 thread priority : 5
 
t3 thread priority : 5
 
t1 thread priority : 2
 
t2 thread priority : 5
 
t3 thread priority : 8
 
Currently Executing Thread : main
 
Main thread priority : 5
 
Main thread priority : 10
 
Output explanation:
 
  
Thread with the highest priority will get an execution chance prior to other threads. Suppose there are 3 threads t1, t2, and t3 with priorities 4, 6, and 1. So, thread t2 will execute first based on maximum priority 6 after that t1 will execute and then t3.
 
The default priority for the main thread is always 5, it can be changed later. The default priority for all other threads depends on the priority of the parent thread.
 
Now geeks you must be wondering out what if we do assign the same priorities to threads than what will happen. All the processing in order to look after threads is carried with help of the thread scheduler. One can refer to the below example of what will happen if the priorities are set to the same and later onwards we will discuss it as an output explanation to have a better understanding conceptually and practically.
 
  
Example
+
Contoh
  
 +
// Java program to demonstrate that a Child thread
 +
// Getting Same Priority as Parent thread
 +
 
 +
// Importing all classes from java.lang package
 +
import java.lang.*;
 +
 
 +
// Main class
 +
// ThreadDemo
 +
// Extending Thread class
 +
class GFG extends Thread {
 +
 
 +
    // Method 1
 +
    // run() method for the thread that is
 +
    // invoked as threads are started
 +
    public void run()
 +
    {
 +
        // Print statement
 +
        System.out.println("Inside run method");
 +
    }
 +
 
 +
    // Method 2
 +
    // Main driver method
 +
    public static void main(String[] args)
 +
    {
 +
        // main thread priority is set to 6 now
 +
        Thread.currentThread().setPriority(6);
 +
 
 +
        // Current thread is accessed
 +
        // using currentThread() method
 +
 
 +
        // Print and display main thread priority
 +
        // using getPriority() method of Thread class
 +
        System.out.println(
 +
            "main thread priority : "
 +
            + Thread.currentThread().getPriority());
 +
 
 +
        // Creating a thread by creating object inside
 +
        // main()
 +
        GFG t1 = new GFG();
 +
 
 +
        // t1 thread is child of main thread
 +
        // so t1 thread will also have priority 6
 +
 
 +
        // Print and display priority of current thread
 +
        System.out.println("t1 thread priority : "
 +
                            + t1.getPriority());
 +
    }
 +
}
  
// Java program to demonstrate that a Child thread
 
// Getting Same Priority as Parent thread
 
 
// Importing all classes from java.lang package
 
import java.lang.*;
 
 
// Main class
 
// ThreadDemo
 
// Extending Thread class
 
class GFG extends Thread {
 
 
    // Method 1
 
    // run() method for the thread that is
 
    // invoked as threads are started
 
    public void run()
 
    {
 
        // Print statement
 
        System.out.println("Inside run method");
 
    }
 
 
    // Method 2
 
    // Main driver method
 
    public static void main(String[] args)
 
    {
 
        // main thread priority is set to 6 now
 
        Thread.currentThread().setPriority(6);
 
 
        // Current thread is accessed
 
        // using currentThread() method
 
 
        // Print and display main thread priority
 
        // using getPriority() method of Thread class
 
        System.out.println(
 
            "main thread priority : "
 
            + Thread.currentThread().getPriority());
 
 
        // Creating a thread by creating object inside
 
        // main()
 
        GFG t1 = new GFG();
 
 
        // t1 thread is child of main thread
 
        // so t1 thread will also have priority 6
 
 
        // Print and display priority of current thread
 
        System.out.println("t1 thread priority : "
 
                          + t1.getPriority());
 
    }
 
}
 
 
Output
 
Output
main thread priority : 6
+
main thread priority : 6
t1 thread priority : 6
+
t1 thread priority : 6
Output explanation:
 
  
If two threads have the same priority then we can’t expect which thread will execute first. It depends on the thread scheduler’s algorithm(Round-Robin, First Come First Serve, etc)
+
Penjelasan Output:
If we are using thread priority for thread scheduling then we should always keep in mind that the underlying platform should provide support for scheduling based on thread priority.
+
 
 +
* Jika dua thread memiliki prioritas yang sama maka kami tidak dapat mengharapkan thread mana yang akan dieksekusi lebih dulu. Itu tergantung pada algoritma penjadwal thread (Round-Robin, First Come First Serve, dll)
 +
* Jika kita menggunakan prioritas thread untuk penjadwalan thread, maka kita harus selalu ingat bahwa platform yang mendasarinya harus memberikan dukungan untuk penjadwalan berdasarkan prioritas thread.
  
  
Line 187: Line 189:
  
 
* https://www.geeksforgeeks.org/java-thread-priority-multithreading/
 
* https://www.geeksforgeeks.org/java-thread-priority-multithreading/
As we already know java being completely object-oriented works within a multithreading environment in which thread scheduler assigns the processor to a thread based on the priority of thread. Whenever we create a thread in Java, it always has some priority assigned to it. Priority can either be given by JVM while creating the thread or it can be given by the programmer explicitly.
 
 
Priorities in threads is a concept where each thread is having a priority which in layman’s language one can say every object is having priority here which is represented by numbers ranging from 1 to 10.
 
 
The default priority is set to 5 as excepted.
 
Minimum priority is set to 1.
 
Maximum priority is set to 10.
 
Here 3 constants are defined in it namely as follows:
 
 
public static int NORM_PRIORITY
 
public static int MIN_PRIORITY
 
public static int MAX_PRIORITY
 
Let us discuss it with an example to get how internally the work is getting executed. Here we will be using the knowledge gathered above as follows:
 
 
We will use currentThread() method to get the name of the current thread. User can also use setName() method if he/she wants to make names of thread as per choice for understanding purposes.
 
getName() method will be used to get the name of the thread.
 
The accepted value of priority for a thread is in the range of 1 to 10.
 
 
Let us do discuss how to get and set priority of a thread in java.
 
 
public final int getPriority(): java.lang.Thread.getPriority() method returns priority of given thread.
 
public final void setPriority(int newPriority): java.lang.Thread.setPriority() method changes the priority of thread to the value newPriority. This method throws IllegalArgumentException if value of parameter newPriority goes beyond minimum(1) and maximum(10) limit.
 
Example
 
 
 
// Java Program to Illustrate Priorities in Multithreading
 
// via help of getPriority() and setPriority() method
 
 
// Importing required classes
 
import java.lang.*;
 
 
// Main class
 
class ThreadDemo 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("Inside run method");
 
    }
 
 
    // Main driver method
 
    public static void main(String[] args)
 
    {
 
        // Creating random threads
 
        // with the help of above class
 
        ThreadDemo t1 = new ThreadDemo();
 
        ThreadDemo t2 = new ThreadDemo();
 
        ThreadDemo t3 = new ThreadDemo();
 
 
        // 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(2);
 
        t2.setPriority(5);
 
        t3.setPriority(8);
 
 
        // t3.setPriority(21); will throw
 
        // IllegalArgumentException
 
 
        // 2
 
        System.out.println("t1 thread priority : "
 
                          + t1.getPriority());
 
 
        // 5
 
        System.out.println("t2 thread priority : "
 
                          + t2.getPriority());
 
 
        // 8
 
        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());
 
    }
 
}
 
Output
 
t1 thread priority : 5
 
t2 thread priority : 5
 
t3 thread priority : 5
 
t1 thread priority : 2
 
t2 thread priority : 5
 
t3 thread priority : 8
 
Currently Executing Thread : main
 
Main thread priority : 5
 
Main thread priority : 10
 
Output explanation:
 
 
Thread with the highest priority will get an execution chance prior to other threads. Suppose there are 3 threads t1, t2, and t3 with priorities 4, 6, and 1. So, thread t2 will execute first based on maximum priority 6 after that t1 will execute and then t3.
 
The default priority for the main thread is always 5, it can be changed later. The default priority for all other threads depends on the priority of the parent thread.
 
Now geeks you must be wondering out what if we do assign the same priorities to threads than what will happen. All the processing in order to look after threads is carried with help of the thread scheduler. One can refer to the below example of what will happen if the priorities are set to the same and later onwards we will discuss it as an output explanation to have a better understanding conceptually and practically.
 
 
Example
 
 
 
// Java program to demonstrate that a Child thread
 
// Getting Same Priority as Parent thread
 
 
// Importing all classes from java.lang package
 
import java.lang.*;
 
 
// Main class
 
// ThreadDemo
 
// Extending Thread class
 
class GFG extends Thread {
 
 
    // Method 1
 
    // run() method for the thread that is
 
    // invoked as threads are started
 
    public void run()
 
    {
 
        // Print statement
 
        System.out.println("Inside run method");
 
    }
 
 
    // Method 2
 
    // Main driver method
 
    public static void main(String[] args)
 
    {
 
        // main thread priority is set to 6 now
 
        Thread.currentThread().setPriority(6);
 
 
        // Current thread is accessed
 
        // using currentThread() method
 
 
        // Print and display main thread priority
 
        // using getPriority() method of Thread class
 
        System.out.println(
 
            "main thread priority : "
 
            + Thread.currentThread().getPriority());
 
 
        // Creating a thread by creating object inside
 
        // main()
 
        GFG t1 = new GFG();
 
 
        // t1 thread is child of main thread
 
        // so t1 thread will also have priority 6
 
 
        // Print and display priority of current thread
 
        System.out.println("t1 thread priority : "
 
                          + t1.getPriority());
 
    }
 
}
 
Output
 
main thread priority : 6
 
t1 thread priority : 6
 
Output explanation:
 
 
If two threads have the same priority then we can’t expect which thread will execute first. It depends on the thread scheduler’s algorithm(Round-Robin, First Come First Serve, etc)
 
If we are using thread priority for thread scheduling then we should always keep in mind that the underlying platform should provide support for scheduling based on thread priority.
 

Latest revision as of 09:15, 12 May 2022

Seperti yang telah kita ketahui, Java sepenuhnya object-oriented bekerja dalam lingkungan multithreading di mana penjadwal thread menetapkan prosesor ke thread berdasarkan prioritas thread . Setiap kali kita membuat thread di Java, selalu ada prioritas yang ditetapkan padanya. Prioritas dapat diberikan oleh JVM saat membuat thread atau dapat diberikan oleh programmer secara eksplisit.

Prioritas dalam thread adalah sebuah konsep di mana setiap thread memiliki prioritas yang dalam bahasa awam bisa dikatakan setiap object memiliki prioritas di sini yang diwakili oleh angka mulai dari 1 hingga 10.

  • Default priority diatur ke 5 sebagai pengecualian.
  • Minimum priority diatur ke 1.
  • Maximum priority diatur ke 10.

Berikut 3 konstanta yang didefinisikan di dalamnya yaitu sebagai berikut:

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

Mari kita diskusikan dengan sebuah contoh untuk mengetahui bagaimana pekerjaan itu dijalankan secara internal. Di sini kita akan menggunakan pengetahuan yang dikumpulkan di atas sebagai berikut:

  • Kita akan menggunakan method currentThread() untuk mendapatkan nama thread saat ini. Pengguna juga dapat menggunakan method setName() jika dia ingin membuat nama thread sesuai pilihan untuk tujuan pemahaman.
  • method getName() akan digunakan untuk mendapatkan nama thread.

Nilai prioritas yang diterima untuk thread berada dalam kisaran 1 hingga 10.

Mari kita bahas cara mendapatkan dan menetapkan prioritas thread di java.

  • public final int getPriority(): java.lang.Thread.getPriority() method mengembalikan prioritas dari thread yang diberikan.
  • public final void setPriority(int newPriority): java.lang.Thread.setPriority() method mengubah prioritas thread menjadi nilai newPriority. Method ini melempar IllegalArgumentException jika nilai parameter newPriority melampaui batas minimum(1) dan maksimum(10).

Contoh

// Java Program to Illustrate Priorities in Multithreading
// via help of getPriority() and setPriority() method
 
// Importing required classes
import java.lang.*;
 
// Main class
class ThreadDemo 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("Inside run method");
    }
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating random threads
        // with the help of above class
        ThreadDemo t1 = new ThreadDemo();
        ThreadDemo t2 = new ThreadDemo();
        ThreadDemo t3 = new ThreadDemo();
 
        // 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(2);
        t2.setPriority(5);
        t3.setPriority(8);
 
        // t3.setPriority(21); will throw
        // IllegalArgumentException
 
        // 2
        System.out.println("t1 thread priority : "
                           + t1.getPriority());
 
        // 5
        System.out.println("t2 thread priority : "
                           + t2.getPriority());
 
        // 8
        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());
    }
}

Output

t1 thread priority : 5
t2 thread priority : 5
t3 thread priority : 5
t1 thread priority : 2
t2 thread priority : 5
t3 thread priority : 8
Currently Executing Thread : main
Main thread priority : 5
Main thread priority : 10

Penjelasan Output:

  • Thread dengan prioritas tertinggi akan mendapatkan kesempatan eksekusi sebelum thread lainnya. Misalkan ada 3 thread t1, t2, dan t3 dengan prioritas 4, 6, dan 1. Jadi, thread t2 akan dieksekusi terlebih dahulu berdasarkan prioritas maksimum 6 setelah itu t1 akan dieksekusi dan kemudian t3.
  • Prioritas default untuk Thread utama selalu 5, dapat diubah nanti. Prioritas default untuk semua Thread lainnya bergantung pada prioritas Thread induk.

Kita biasanya bertanya-tanya bagaimana cara kita menetapkan prioritas yang sama untuk thread daripada apa yang akan terjadi. Semua pemrosesan untuk menjaga thread dilakukan dengan bantuan penjadwal thread. Kita dapat merujuk pada contoh di bawah ini tentang apa yang akan terjadi jika prioritas ditetapkan sama dan selanjutnya kita akan membahasnya sebagai penjelasan keluaran untuk memiliki pemahaman yang lebih baik secara konseptual dan praktis.


Contoh

// Java program to demonstrate that a Child thread
// Getting Same Priority as Parent thread
 
// Importing all classes from java.lang package
import java.lang.*;
 
// Main class
// ThreadDemo
// Extending Thread class
class GFG extends Thread {
 
    // Method 1
    // run() method for the thread that is
    // invoked as threads are started
    public void run()
    {
        // Print statement
        System.out.println("Inside run method");
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // main thread priority is set to 6 now
        Thread.currentThread().setPriority(6);
 
        // Current thread is accessed
        // using currentThread() method
 
        // Print and display main thread priority
        // using getPriority() method of Thread class
        System.out.println(
            "main thread priority : "
            + Thread.currentThread().getPriority());
 
        // Creating a thread by creating object inside
        // main()
        GFG t1 = new GFG();
 
        // t1 thread is child of main thread
        // so t1 thread will also have priority 6
 
        // Print and display priority of current thread
        System.out.println("t1 thread priority : "
                           + t1.getPriority());
    }
}

Output

main thread priority : 6
t1 thread priority : 6

Penjelasan Output:

  • Jika dua thread memiliki prioritas yang sama maka kami tidak dapat mengharapkan thread mana yang akan dieksekusi lebih dulu. Itu tergantung pada algoritma penjadwal thread (Round-Robin, First Come First Serve, dll)
  • Jika kita menggunakan prioritas thread untuk penjadwalan thread, maka kita harus selalu ingat bahwa platform yang mendasarinya harus memberikan dukungan untuk penjadwalan berdasarkan prioritas thread.


Referensi