Pages

Sunday 27 October 2013

Set and get the Thread Priorities in Jav



          The thread priorities are the real numbers used by thread scheduler to decide when each thread should be allowed to execute. The range of thread priority is within the MIN_PRIORITY and MAX_PRIORITY. The default value or normal priority is NORM_PRIORITY. The below table list the values of the maximum, minimum and default priorities in Java.
            
Keyword
Real Number Representation
MIN_PRIORITY
1
MAX_PRIORITY
10
NORM_PRIORITY
5
        
          The default priority of any thread in Java will be NORM_PRIORITY i.e. the value 5. You can set the priorities of any thread to different desired values within the range by using the method setPriority() of Thread. The general form of this method is shown below.

final void setPriority ( int level);


          The value of the current priority of any thread can be obtained by using the getPriority() method of Thread. The general form of this method is shown below.

final int getPriority (); 


          The below example program shows how to set and get the thread priorities.

package blog.exception.mainThread;

public class MyThreadPriority implements Runnable
{
  String ThreadName;
  int priorityValue;
  Thread th;
  
  public MyThreadPriority(String ThreadName,int priorityValue)
  {
                this.ThreadName=ThreadName;
                this.priorityValue=priorityValue;
                th=new Thread(this,this.ThreadName);
               
                th.setPriority(this.priorityValue);// setting thread priority
                System.out.println();
                System.out.println("Thread Priority of "+this.ThreadName+" is "+th.getPriority());//getting thread priority using method.
                th.start();
  }
  // entry point for the thread.
 
  @Override
                public void run()
    {
                                try
                                {
                                                for(int i=5;i>0;i--)
                                                {
                                                                System.out.println(ThreadName+ ":"+ i);
                                                                Thread.sleep(500);
                                                }
                                }
                                catch(Exception ex)
                                {
                                                ex.printStackTrace();
                                }
                  System.out.println("Am exiting :"+ThreadName);         
                }
 
}

package blog.exception.mainThread;

public class DemoThreadPriority
{
   public static void main(String[] args)
   {
                new MyThreadPriority("Child1", 10);
                new MyThreadPriority("Child2",1);
                new MyThreadPriority("Child3", 5);
  
  
     try
     {
                   //main thread
                   Thread.sleep(10000);
     }
     catch (Exception ex)
     {
                 ex.printStackTrace();
     }
    
     System.out.println("Main thread exiting");
   }
}


Output:

Thread Priority of Child1 is 10

Thread Priority of Child2 is 1
Child1:5

Thread Priority of Child3 is 5
Child2:5
Child3:5
Child3:4
Child2:4
Child1:4
Child1:3
Child3:3
Child2:3
Child3:2
Child1:2
Child2:2
Child3:1
Child2:1
Child1:1
Am exiting :Child1
Am exiting :Child2
Am exiting :Child3
Main thread exiting

           The thread priorities are used to decide when to switch from one running thread to the next thread. This switching is called context switching.

Friday 18 October 2013

Second approach to create a Thread



    In my previous post I have illustrated, how you can create a thread by implementing Runnable interface. In this post I will detail on second approach of thread creation, by extending the Thread class itself.

Steps to create thread by extending Thread class:

1.      Create a new class that extends Thread class.

2.      Override the run () method within the class that extends the Thread class.

3.      Define the start () method within the class that extends the Thread class.

    Consider the below example program where the class MyThread extends the Thread class and creates a new thread called Demo Child Thread.

package blog.exception.mainThread;

public class MyThread extends Thread
{
 
  MyThread()
  {
                  super("Demo Child Thread");// call to constructor of Thread class.
                  System.out.println("My Thread Name "+this);
                  start();
                 
  }
 
  @Override
                public void run()
  {
                try
                {
                                for(int i=3; i>0 ;i--)
                                {
                                                System.out.println("My Child Thread "+i);
                                                Thread.sleep(500);
                                }
                }
               
                catch(InterruptedException ex)
                {
                                System.out.println("error in child "+ex);
                }
  }
}



package blog.exception.mainThread;

public class DemoMyThread
{             
 public static void main(String[] args)
 {
                 new MyThread(); // call to MyThread constructor to create a new thread
                 
                 try
                 {
                                 for(int i=3;i>0;i--)
                                 {
                                                 System.out.println("Main Thread "+i);
                                                 Thread.sleep(1000);
                                 }
                 }
                 catch(InterruptedException ex)
                 {
                                 System.out.println("error in main thread "+ex);
                 }
                 
                 System.out.println("Main Thread Terminating");
 }
}


Output:
My Thread Name Thread[Demo Child Thread,5,main]
Main Thread 3
My Child Thread 3
My Child Thread 2
Main Thread 2
My Child Thread 1
Main Thread 1
Main Thread Terminating