Search Here

Java program for Setting Priority of a Job


/* Priority to Threads */

class Thread_1 extends Thread
{
 Thread_1()
 {
  setName("Thread_1");
  setPriority(1);
 }
 
 public void run()
 {
  for(int i=1;i<=5;i++)
   System.out.println("\tFrom Thread_1: i=" +i);
  System.out.println("Exiting Thread_1");
 } 
}

class Thread_2 extends Thread
{
 Thread_2()
 {
  setName("Thread_2");
  setPriority(5);
 }
 
 public void run()
 {
  for(int j=1;j<=5;j++)
   System.out.println("\tFrom Thread_2: j=" +j);
  System.out.println("Exiting Thread_2");
 }
}

class Thread_3 extends Thread
{
 Thread_3()
 {
  setName("Thread_3");
  setPriority(10);
 }
 
 public void run()
 {
  for(int k=1;k<=5;k++)
   System.out.println("\tFrom Thread_3: k=" +k);
  System.out.println("Exiting Thread_3");
 }
}

class Main
{
 public static void main(String args[])
 {
  Thread_1 t1=new Thread_1();
  Thread_2 t2=new Thread_2();
  Thread_3 t3=new Thread_3();
  
  System.out.println(t1);
  System.out.println(t2);
  System.out.println(t3);
  System.out.println();
  
  System.out.println("Starting Thread_1");
  t1.start();
  System.out.println("Starting Thread_2");
  t2.start();
  System.out.println("Starting Thread_3");
  t3.start();
  
  
  System.out.println("Exiting main thread");
 }
}

JAVA program for setting priority of a job
Output

No comments:

Post a Comment