Thursday, July 21, 2011

How to Create a Pre-emptive Thread Shedular using Java.

First of all To understand this Post You must have some quite good knowledge about the Threads and their behaviors.

Now lets talk about the topic. What is a Preemptive thread scheduler? And why we need it?. The Preemptive scheduler means can suspend a current running thread and run another read and RESUME the previous suspended thread to continue.

So why we need a Preemptive task scheduler. In java JVM(java virtual machine ) has a thread scheduler but it is non preemptive scheduler. So if we need a some kind of Preemptive thread scheduler we have to build it. And we can't control massive complex systems with a non preemptive thread scheduler.

So lets see how can we build a preemptive task scheduler.

First of all we have to maintain a thread list that we are going to manage. It can be a vector , stack or array of threads or a queue. The characteristics of the scheduler will depend on the storing system of the thread list.We need to update a variable(type of thread ) to store the current thread which is at the running state for a given instance. And we must have a method to get the next thread to run.

In this scenario the preemptive thread scheduler it self is a thread which has the highest priority among the handling threads. So it can suspend or pause a any thread because it has the high priority than other threads.

Now Let see a sample code written in java to create a preemptive thread scheduler.


 import java.util.Scanner;  
 import java.util.Vector;  
 import java.util.*;  
 public class PreemtiveThreadShedular extends Thread{  
     private Vector taskList=new Vector(5,5);  
       private Philosopher currentThread;  
     private int index=0;  
     public static void main(String [] args){  
        PreemtiveThreadShedular shedular=new PreemtiveThreadShedular();  
        shedular.initializeThreads();  
         shedular.start();  
         shedular.run();  
     }  
     public PreemtiveThreadShedular(){  
       this.setPriority(MAX_PRIORITY);  
     }  
     public void initializeThreads(){  
         Philosopher [] phi=new Philosopher[num];  
         for(int i=0;i<phi.length;i++){  
           phi[i]=new Philosopher(""+i);  
           phi[i].setPriority(NORM_PRIORITY);     
           phi[i].start();  
           phi[i].suspend();          
           taskList.add(phi[i]);  
         }  
       }  
     public void nextThread(){  
       int size=taskList.size();  
       if(taskList.elementAt(index)!=null){  
         currentThread=(Philosopher) taskList.elementAt(index);  
         currentThread.suspend();  
       }  
       index++;  
       if(index==size){  
         index=0;  
       }  
             currentThread=(Philosopher) taskList.elementAt(index);  
       currentThread.resume();  
       try{   
       currentThread.run();  
       }  
       catch(InterruptedException e){  
         System.out.println("Interrupted Error Occured....");  
       }  
     }  
   public void run(){  
     while(isAlive()){  
      nextThread();  
      try{  
        sleep(100);  
      }  
      catch(InterruptedException e){  
        System.out.println("Error occured While Interrupting....");  
        stop();  
      }  
     }  
   }   
 }  
 class Philosopher extends Thread{  
  private String name;  
  public Philosopher(String name){  
    this.name=name;  
 }  
 public void run(){  
 System.out.println(name+" Thread is running");  
 }  
 }