1 / 17

Thread Priorities

Thread Priorities. Introduction. The 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.

kevyn-lucas
Télécharger la présentation

Thread Priorities

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Thread Priorities

  2. Introduction • The thread scheduler can use the thread priorities in the form of integer valueto 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.   www.ustudy.in

  3. Priorities • Priorities 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).  www.ustudy.in

  4. Description www.ustudy.in

  5. Methods to set the Priority www.ustudy.in

  6. Preemptive Scheduling • When 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.  www.ustudy.in

  7. Priority • If 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. www.ustudy.in

  8. Thread Schedular • Preemptive 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. www.ustudy.in

  9. Example Program classMyThread1 extends Thread { MyThread1(String s) {super(s);     start();   } public voidrun() www.ustudy.in

  10. Cont., {for(inti=0;i<3;i++) { Thread cur=Thread.currentThread();       cur.setPriority(Thread.MIN_PRIORITY);intp=cur.getPriority(); System.out.println("Thread Name  :“ +Thread.currentThread().getName()) www.ustudy.in

  11. Cont., System.out.println("Thread Priority :“ +cur); } } } classMyThread2 extendsThread { MyThread2 extends Thread www.ustudy.in

  12. Cont., { super(s);     start(); } } public voidrun() { www.ustudy.in

  13. Cont., for(inti=0;i<3;i++) {       Thread cur=Thread.currentThread();       cur.setPriority(Thread.MAX_PRIORITY);intp=cur.getPriority(); www.ustudy.in

  14. Cont.,      System.out.println("Thread Name  :"+Thread.currentTh read().getName()); System.out.println("Thread Priority  :"+cur); } } } www.ustudy.in

  15. Cont., public classThreadPriority {public static voidmain(String args[]) {       MyThread1 m1=newMyThread1("My Thread 1");     MyThread2 m2=newMyThread2("My Thread 2");   }} www.ustudy.in

  16. Output of a Program C:\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] www.ustudy.in

  17. The End ….. Thank You ….. www.ustudy.in

More Related