SlideShare a Scribd company logo
2
Most read
6
Most read
16
Most read
Thread Priorities
IntroductionThe thread scheduler can use the thread priorities in the form of integer value to each of its thread to determine the execution schedule of threads . Thread gets the ready-to-run state according to their priorities. The thread scheduler provides the CPU time to thread of highest priority during ready-to-run state.  
PrioritiesPriorities are integer values from 1 (lowest priority given by the constant Thread.MIN_PRIORITY) to 10 (highest priority given by the constant Thread.MAX_PRIORITY). The default priority is 5(Thread.NORM_PRIORITY). 
Description
Methods to set the Priority
Preemptive SchedulingWhen a Java thread is created, it inherits its priority from the thread that created it.  At any given time, when multiple threads are ready to be executed, the runtime system chooses the runnable thread with the highest priority for execution. In Java runtime system, preemptive scheduling algorithm is applied. 
PriorityIf at the execution time a thread with a higher priority and all other threads are runnable then the runtime system chooses the new higher priority thread for execution. On the other hand, if two threads of the same priority are waiting  to be executed by the CPU then the round-robin algorithm is applied in which the scheduler chooses one of them to run according to their round of time-slice.
Thread SchedularPreemptive scheduling – If the new thread has a higher priority then current running thread leaves the runnable state and higher priority thread enter to the runnable state.Time-Sliced (Round-Robin) Scheduling – A running thread is allowed to be execute for the fixed time, after completion the time, current thread indicates to the another thread to enter it in the runnable state.
Example Program	class MyThread1 extends Thread{     MyThread1(String s)    {    super(s);          start();      }public void run()
Cont.,       { for(int i=0;i<3;i++)               {Thread cur=Thread.currentThread();                          cur.setPriority(Thread.MIN_PRIORITY);      int p=cur.getPriority();                          System.out.println("Thread Name  :“+Thread.currentThread().getName())
Cont.,                         System.out.println("Thread Priority :“ +cur);                 }        }}class MyThread2 extends Thread{          MyThread2 extends Thread
Cont.,           {super(s);    start();            }}public void run(){
Cont.,for(int i=0;i<3;i++)      {               Thread cur=Thread.currentThread();               cur.setPriority(Thread.MAX_PRIORITY);      int p=cur.getPriority();
Cont.,                  System.out.println("Thread Name  :“+Thread.currentThread().getName());System.out.println("Thread Priority  :"+cur);           }      }}
Cont.,public class ThreadPriority{public static void main(String args[])        {                   MyThread1 m1=new MyThread1("My Thread 1");                 MyThread2 m2=new MyThread2("My Thread 2");         }}
Output of a ProgramC:\dir>javac ThreadPriority.javaC:\dir>java ThreadPriorityThread Name :My Thread 1Thread Name :My Thread 2Thread Priority :Thread[My Thread 2,10,main]Thread Name :My Thread 2Thread Priority :Thread[My Thread 2,10,main]Thread Name :My Thread 2Thread Priority :Thread[My Thread 2,10,main]Thread Priority :Thread[My Thread 1,1,main]Thread Name :My Thread 1Thread Priority :Thread[My Thread 1,1,main]Thread Name :My Thread 1Thread Priority :Thread[My Thread 1,1,main]
The End                     ….. Thank You …..

More Related Content

PPTX
Thread priority in java
ODP
Multithreading In Java
PPTX
Insertion in singly linked list
PPTX
2 phase locking protocol DBMS
PPTX
daa-unit-3-greedy method
PPTX
Polymorphism
PPTX
Network Layer design Issues.pptx
PPTX
Inheritance in java
Thread priority in java
Multithreading In Java
Insertion in singly linked list
2 phase locking protocol DBMS
daa-unit-3-greedy method
Polymorphism
Network Layer design Issues.pptx
Inheritance in java

What's hot (20)

PDF
IP Datagram Structure
PPTX
Static keyword ppt
PPT
Routing algorithm network layer
PPTX
This keyword in java
PPTX
Function overloading and overriding
PPTX
Mealy and moore machine
PPTX
Timestamp protocols
PPT
Scheduling algorithms
PPTX
Shortest Job First
PPT
Distributed Deadlock Detection.ppt
PPTX
Command line arguments
PPT
Arrays searching-sorting
PPT
Applet life cycle
PPTX
Optimal Binary Search tree ppt seminar.pptx
PPT
Method overriding
PPT
Minimization of DFA
PPTX
Shortest job first Scheduling (SJF)
PDF
Java threads
PPTX
Constructor overloading & method overloading
IP Datagram Structure
Static keyword ppt
Routing algorithm network layer
This keyword in java
Function overloading and overriding
Mealy and moore machine
Timestamp protocols
Scheduling algorithms
Shortest Job First
Distributed Deadlock Detection.ppt
Command line arguments
Arrays searching-sorting
Applet life cycle
Optimal Binary Search tree ppt seminar.pptx
Method overriding
Minimization of DFA
Shortest job first Scheduling (SJF)
Java threads
Constructor overloading & method overloading
Ad

Viewers also liked (6)

PPT
Thread priorities35
PPT
Java thread
PDF
javathreads
PDF
Java Thread Synchronization
PDF
Programming with Threads in Java
PPT
Java multi threading
Thread priorities35
Java thread
javathreads
Java Thread Synchronization
Programming with Threads in Java
Java multi threading
Ad

Similar to Thread priorities (20)

PPTX
14 thread
PPT
Md09 multithreading
PPT
Multithreading
PPTX
multithreading to be used in java with good programs.pptx
PPTX
multithreading.pptx
PPTX
Multithreading in java
PPTX
Multithreading in java
PPT
Threads in java
PPTX
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
PPT
Session 7_MULTITHREADING in java example.ppt
PPTX
Multi threading
PPT
Threads
PPT
Java Performance, Threading and Concurrent Data Structures
PPTX
Java Multithreading.pptx
PPTX
MSBTE Computer Engineering JPR java. multi. threading.pptx
PDF
JAVA 3.2.pdfhdfkjhdfvbjdbjfhjdfhdjhfjdfdjfhdjhjd
PPTX
Multithreading programming in java
PPTX
Chap3 multi threaded programming
PPTX
Multithreading.pptx
14 thread
Md09 multithreading
Multithreading
multithreading to be used in java with good programs.pptx
multithreading.pptx
Multithreading in java
Multithreading in java
Threads in java
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
Session 7_MULTITHREADING in java example.ppt
Multi threading
Threads
Java Performance, Threading and Concurrent Data Structures
Java Multithreading.pptx
MSBTE Computer Engineering JPR java. multi. threading.pptx
JAVA 3.2.pdfhdfkjhdfvbjdbjfhjdfhdjhfjdfdjfhdjhjd
Multithreading programming in java
Chap3 multi threaded programming
Multithreading.pptx

More from Then Murugeshwari (20)

PPT
Traffic safety
PPT
P h indicators
PPT
Avogadro's law
PPT
PPT
Microwave remote sensing
PPT
Newton's law
PPT
Surface tension
PPT
Hook's law
PPT
Hook's law
PPTX
ERP components
PPTX
Database fundamentals
PPTX
PPTX
PPTX
Bluetooth profile
PPTX
PPTX
Operators in java
PPTX
PPTX
Identifiers
PPT
Virtual ground
Traffic safety
P h indicators
Avogadro's law
Microwave remote sensing
Newton's law
Surface tension
Hook's law
Hook's law
ERP components
Database fundamentals
Bluetooth profile
Operators in java
Identifiers
Virtual ground

Recently uploaded (20)

PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Spectroscopy.pptx food analysis technology
PDF
Machine learning based COVID-19 study performance prediction
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Getting Started with Data Integration: FME Form 101
PPTX
Tartificialntelligence_presentation.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
A Presentation on Artificial Intelligence
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Spectroscopy.pptx food analysis technology
Machine learning based COVID-19 study performance prediction
Univ-Connecticut-ChatGPT-Presentaion.pdf
NewMind AI Weekly Chronicles - August'25-Week II
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Group 1 Presentation -Planning and Decision Making .pptx
A comparative study of natural language inference in Swahili using monolingua...
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Unlocking AI with Model Context Protocol (MCP)
Encapsulation_ Review paper, used for researhc scholars
Getting Started with Data Integration: FME Form 101
Tartificialntelligence_presentation.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Network Security Unit 5.pdf for BCA BBA.
Reach Out and Touch Someone: Haptics and Empathic Computing
A Presentation on Artificial Intelligence
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf

Thread priorities

  • 2. IntroductionThe thread scheduler can use the thread priorities in the form of integer value to each of its thread to determine the execution schedule of threads . Thread gets the ready-to-run state according to their priorities. The thread scheduler provides the CPU time to thread of highest priority during ready-to-run state.  
  • 3. PrioritiesPriorities are integer values from 1 (lowest priority given by the constant Thread.MIN_PRIORITY) to 10 (highest priority given by the constant Thread.MAX_PRIORITY). The default priority is 5(Thread.NORM_PRIORITY). 
  • 5. Methods to set the Priority
  • 6. Preemptive SchedulingWhen a Java thread is created, it inherits its priority from the thread that created it.  At any given time, when multiple threads are ready to be executed, the runtime system chooses the runnable thread with the highest priority for execution. In Java runtime system, preemptive scheduling algorithm is applied. 
  • 7. PriorityIf at the execution time a thread with a higher priority and all other threads are runnable then the runtime system chooses the new higher priority thread for execution. On the other hand, if two threads of the same priority are waiting  to be executed by the CPU then the round-robin algorithm is applied in which the scheduler chooses one of them to run according to their round of time-slice.
  • 8. Thread SchedularPreemptive scheduling – If the new thread has a higher priority then current running thread leaves the runnable state and higher priority thread enter to the runnable state.Time-Sliced (Round-Robin) Scheduling – A running thread is allowed to be execute for the fixed time, after completion the time, current thread indicates to the another thread to enter it in the runnable state.
  • 9. Example Program class MyThread1 extends Thread{ MyThread1(String s) {    super(s);     start();   }public void run()
  • 10. Cont., { for(int i=0;i<3;i++) {Thread cur=Thread.currentThread();       cur.setPriority(Thread.MIN_PRIORITY);      int p=cur.getPriority(); System.out.println("Thread Name  :“+Thread.currentThread().getName())
  • 11. Cont., System.out.println("Thread Priority :“ +cur); } }}class MyThread2 extends Thread{ MyThread2 extends Thread
  • 12. Cont., {super(s);    start(); }}public void run(){
  • 13. Cont.,for(int i=0;i<3;i++) {       Thread cur=Thread.currentThread();       cur.setPriority(Thread.MAX_PRIORITY);      int p=cur.getPriority();
  • 14. Cont.,      System.out.println("Thread Name  :“+Thread.currentThread().getName());System.out.println("Thread Priority  :"+cur); } }}
  • 15. Cont.,public class ThreadPriority{public static void main(String args[]) {       MyThread1 m1=new MyThread1("My Thread 1");     MyThread2 m2=new MyThread2("My Thread 2");   }}
  • 16. Output of a ProgramC:\dir>javac ThreadPriority.javaC:\dir>java ThreadPriorityThread Name :My Thread 1Thread Name :My Thread 2Thread Priority :Thread[My Thread 2,10,main]Thread Name :My Thread 2Thread Priority :Thread[My Thread 2,10,main]Thread Name :My Thread 2Thread Priority :Thread[My Thread 2,10,main]Thread Priority :Thread[My Thread 1,1,main]Thread Name :My Thread 1Thread Priority :Thread[My Thread 1,1,main]Thread Name :My Thread 1Thread Priority :Thread[My Thread 1,1,main]
  • 17. The End ….. Thank You …..