Thread Priority in Multi Threading Process

What is Thread Priority in Java Multi Threading Process?

In multi threading process, we have to check which process is more important and which process is less important. We can set 1 to 10 Thread priority of any process.

What are the value of MAX_PRIORITY, MIN_PRIORITY and NORM_PRIORITY?

MAX_PRIORITY = 10

MIN_PRIORITY = 1

NORM_PRIORITY = 5

 

In this program, we will see how to MAX_PRIORITY’s process works faster than MIN_PRIORITY.

 

A class in classStore1 package

package classStore1;

public class A implements Runnable

{

            public Thread t;

            public int p;

            public int x = 1;

            public boolean b = true;

            public A(int p)

            {

                        t = new Thread(this);

                        this.p = p;

                        t.setPriority(this.p);// set each thread priority

                        t.start();

            }

            public void run()

            {

                        while(b)

                        {

                                    x++;// increase the value of x

                        }

            }

            public void stop()

            {

                        b = false;

            }

}

 

ThreadEx7 class in examples Package

package examples;

import classStore1.*;

public class ThreadEx7

{

                public static void main(String[] args)

                {

                                // Create constructor and pass priority

                                A a1 = new A(10); // Thread.MAX_PRIORITY

                                A a2 = new A(Thread.MIN_PRIORITY);// 1

 

                                // Call to Process

                                try

                                {

                                                // Main Program in Sleep mode

                                                Thread.sleep(3000);

                                }

                                catch(Exception ex){}

 

                                a1.stop(); // stop the process 1

                                a2.stop();  // stop the process2

 

                                System.out.println(“Process 1 : ” + a1.x); // value max -> priority 10

                                System.out.println(“Process 2 : ” + a2.x); // value min -> priority 1

                }

}

 

Video also available on YouTube Java Program Channel


[sbscrbr_form]

If you have any query on blog example, you can directly comment us or subscribe our website


Earn Money Online

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

255total visits,0visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

EmailEmail
PrintPrint