Thread Priority in Java

Advertisements

Thread Priority

Thread Scheduler(part of JVM) handles the context switching of threads with same priority. The priority of thread lies between 1 to 10. Default priority of each thread is NORM_PRIORITY, which is 5. Thread priority can be defined as:

  • MAX_PRIORITY, which is 10
  • NORM_PRIORITY, which is 5
  • MIN_PRIORITY, which is 1

We can get and set the priority of thread using Thread.getPriority() method and Thread.setPriority() method.

Syntax of setting thread priority


    Thread.setPriority(Priority value);

Example of setting thread priority


//ThreadDemo.java

    class Pattern
    {

        int A=0;
        int B=0;

        boolean running=true;

        public void CountA()
        {
            while(running)
            A++;
        }

        public void CountB()
        {
            while(running)
            B++;
        }

        public void Stop()
        {
            running = false;
        }

        public void Show()
        {
            System.out.print("\n\tA : " + A);
            System.out.print("\n\tB : " + B);
        }

    }

    class ThreadA extends Thread
    {
        Pattern P;

        ThreadA(Pattern P)
        {
        this.P = P;
        }

        public void run()
        {
        P.CountA();
        }
    }

    class ThreadB extends Thread
    {
        Pattern P;

        ThreadB(Pattern P)
        {
            this.P = P;
        }

        public void run()
        {
            P.CountB();
        }
    }

    class ThreadDemo
    {
        public static void main(String arg[])
        {

            Pattern P = new Pattern();

            ThreadA t1 = new ThreadA(P);    //Statement 1
            ThreadB t2 = new ThreadB(P);    //Statement 2


            t1.setPriority(Thread.MAX_PRIORITY);
            t2.setPriority(Thread.MIN_PRIORITY);

            t1.start();
            t2.start();


            try
            {
                Thread.sleep(10000);
            }
            catch (Exception e)
            {
                System.out.println("\n\tError.....");
            }


            P.Stop();
            P.Show();

        }
    }
    Output 1:

                CountA : 514282053
                CountB : 351280945

    Output 2:

                CountA : 459446959
                CountB : 293012252

    Output 3:

                CountA : 487615809
                CountB : 561171867

    Output 4:

                CountA : 663687852
                CountB : 516291820


The output shows that most of the time higher priority thread gets the higher execution time.

In the above example, we have defined a common class Pattern and pass the object P of Pattern class to the derived classes such as ThreadA, ThreadB as shown in statement 1 and 2.

Pattern class has two methods CountA() and CountB(), which will increment the value of A and B respectively.

ThreadA will invoke CountA() method of Pattern class.

ThreadB will invoke CountB() method of Pattern class.

After starting two child threads, we have blocked the main thread for 10000 milliseconds(10 seconds) so that infinite loop inside CountA() and CountB() methods will get enough time to execute.

After 10 seconds we have stopped the child threads and displayed the value of A and B.

Advertisement