Thread priority is java is topic of multithreading in java. Thread scheduler choose which thread to execute first from the ready thread on the bases of priority. You can watch a video on this topic on https://www.youtube.com/watch?v=xopkQL5tpb4&t=693s. Thread priority in java use getPriorit...
Thread priority is java is topic of multithreading in java. Thread scheduler choose which thread to execute first from the ready thread on the bases of priority. You can watch a video on this topic on https://www.youtube.com/watch?v=xopkQL5tpb4&t=693s. Thread priority in java use getPriority and setPriority method for priority manipulation.
Size: 240.88 KB
Language: en
Added: Jun 02, 2020
Slides: 8 pages
Slide Content
Thread Priority in Java CPU Thread with priority 2 Thread with priority 5 Thread with priority 9 Thread with priority 7 Video link for this topic : https://www.youtube.com/watch?v=xopkQL5tpb4&t=693s
Thread Priority Thread Priority is a number assigned to every thread, on which thread scheduler takes the decision, which thread executes first amongst multiple threads. Whenever the thread-scheduler pick a new thread, from the ready thread, it picks the highest-priority thread. We can set the priority of each thread . Video link for this topic : https://www.youtube.com/watch?v=xopkQL5tpb4&t=693s
Java Thread Priority In Java each thread inherits its priority by its parent thread. Every thread have priority by default i.e. 5. We can increase or decrease the thread priority by built-in method of Thread class of Java. We can set the priority of thread from 1 to 10. Where 10 is the highest priority and 1 is the least priority. Video link for this topic : https://www.youtube.com/watch?v=xopkQL5tpb4&t=693s
Method for the Priority getPriority () This method is used to know the priority of a thread. e.g. System.out.println ( threadName.getPriority ()) Video link for this topic : https://www.youtube.com/watch?v=xopkQL5tpb4&t=693s
Method for the Priority setPriority () It is used to set the priority from 1 to 10 to a particular thread. e.g. threadName.setPriority (4); Video link for this topic : https://www.youtube.com/watch?v=xopkQL5tpb4&t=693s
Java Constant for Priority Following are the in built constant for priorities: Constant Priority MAX_PRIORITY 10 MIN_PRIORITY 1 NORM_PRIORITY 5 e.g. for setting the priority using constant: threadName.setPriority ( Thread.MAX_PRIORITY ); threadName.setPriority ( Thread.MIN_PRIORITY ); Video link for this topic : https://www.youtube.com/watch?v=xopkQL5tpb4&t=693s
Limitation of Java Thread Priority Rule of thread priority is system dependent (OS) Some OS have very few priority level than Java i.e. 10. In this case there is no use to set the high priority upto 10. When there is more than 1 thread of same priority then scheduler of OS pick anyone as per the scheduling algorithm. Video link for this topic : https://www.youtube.com/watch?v=xopkQL5tpb4&t=693s
public class CheckRunnable { public static void main(String args []) { AThread1 obj1 = new AThread1(); BThread1 obj2 = new BThread1(); CThread1 obj3 = new CThread1(); Thread. currentThread (). setPriority (8); Thread Athread = new Thread(obj1); Thread Bthread = new Thread(obj2); Thread Cthread = new Thread(obj3); System. out.println ( Athread.getPriority ()); System. out.println ( Bthread.getPriority ()); System. out.println ( Cthread.getPriority ()); System. out.println ( Thread.currentThread (). getPriority ()); Bthread.setPriority ( Thread. MIN_PRIORITY ); System. out.println ( Bthread.getPriority ()); Athread.start (); Bthread.start (); Cthread.start (); } } class AThread1 implements Runnable { public void run() { for( int i = 1;i<=5;i++) { System. out.println (" Athread " + " " + i ); try { Thread. sleep (500 ); } catch(Exception e) {}}}} class BThread1 implements Runnable { public void run() { System. out.println (" BThread "); }} class CThread1 implements Runnable { public void run() { System. out.println (" CThread "); } } Program to set priority of current thread/parent thread and use of constant Video link for this topic : https://www.youtube.com/watch?v=xopkQL5tpb4&t=693s