Thread Priority


public class ThreadPriority extends Thread {
   
    public void run (){
        try {
            for (int i = 0; i < 4; i++) {
                 Thread.sleep(1000);
                  System.out.println("The current thread is  " +i+"  ," +Thread.currentThread().getName());
            }
         
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadPriority.class.getName()).log(Level.SEVERE, null, ex);
        }
 
}
    public static void main(String[] args) {
        ThreadPriority obj = new ThreadPriority();
         ThreadPriority obj1 = new ThreadPriority();
        obj.setPriority(Thread.MIN_PRIORITY);
        obj1.setPriority(Thread.MAX_PRIORITY);
       
        obj.start();
        obj1.start();
        //its all about priotising the threads
    }
   
}

output

The current thread is  0  ,Thread-1
The current thread is  0  ,Thread-0
The current thread is  1  ,Thread-1
The current thread is  1  ,Thread-0
The current thread is  2  ,Thread-1
The current thread is  2  ,Thread-0
The current thread is  3  ,Thread-1
The current thread is  3  ,Thread-0
BUILD SUCCESSFUL (total time: 4 seconds)


//see the priority of ececution of thread 1 .. it always start before thread 0

No comments:

Post a Comment