Thread Priorities in java Programming

Java thread priority is one of the important concepts in java thread. Every thread created has some priority. Threads are executed according to their priority. Threads with higher priority are executed before threads with lower priority. A newly created thread has same priority as the thread that creates it.
Java thread scheduler uses the thread priority in the form of an integer value, it is used to determine the execution schedule of a thread. Threads with higher priority get the CPU time first.
Java thread priorities are integer values ranging from 1 to 10. 1 is the lowest priority and 10 is the highest priority. The default priority of a thread is 5.
The thread priorities are assigned to some constant literals as follows,
  1. Thread.MIN_PRIORITY : It is the minimum priority of any thread i.e. 1
  2. Thread.MAX_PRIORITY : It is the maximum priority of any thread i.e. 10
  3. Thread.NORM_PRIORITY : It is the normal and default priority of ant thread i.e. 5
To set the thread priorities we can use setPriority() method. Its syntax is,
final void setPriority(int level);
To obtain the current priority of a thread we can use getPriority() method. Its syntax is,
final int getPriority();
 
 
 

Setting thread priorities

Setting a threads priority can be very useful if one thread has more critical tasks to perform than another.
The Thread class has a method called setPriority(int level) with which you can alter the priority a Thread instance has.
The priority level range from 1 (least important) to 10 (most important) and if no level is explicitly set, a Thread instance has the priority level of 5.
In the first example below no priorites are set, so both threads have the priority level 5. The TestThread class implements the Runnable interface and in its
run() method loops from 1 to 10 and output the number along with its Thread id, which is passed to the constructor.

 Program Main.java


public class Main
{
  
  

    public void setPrioritiesOnThreads()
    {
      
        Thread thread1 = new Thread(new TestThread(1));
        Thread thread2 = new Thread(new TestThread(2));
      
        //Setting priorities on the Thread objects
        thread1.setPriority(Thread.MAX_PRIORITY);
        thread2.setPriority(Thread.MIN_PRIORITY);
      
        thread1.start();
        thread2.start();
      
        try {
          
            //Wait for the threads to finish
            thread1.join();
            thread2.join();
          
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
      
        System.out.println("Done.");
      
      
    }
  

    public static void main(String[] args)
    {
        new Main().setPrioritiesOnThreads();
    }
  
  
    class TestThread implements Runnable
     {
      
        int id;
      
        public TestThread(int id)
        {
          
            this.id = id;
        }
      
        public void run()
        {
          
            for (int i = 1; i <= 10; i++)
            {
                System.out.println("Thread" + id + ": " + i);
            }
        }
    }
}



Output 

G:\>javac Main.java
G:\>java Main
Thread2: 1
Thread1: 1
Thread2: 2
Thread1: 2
Thread2: 3
Thread1: 3
Thread2: 4
Thread1: 4
Thread2: 5
Thread1: 5
Thread2: 6
Thread1: 6
Thread2: 7
Thread2: 8
Thread1: 7
Thread2: 9
Thread1: 8
Thread2: 10
Thread1: 9
Thread1: 10
Done.



Thread Priorities in java Programming

 

0 comments:

Post a Comment

 

learn java programming Copyright © 2011-2012 | Powered by appsackel.org