Hi, I want to know about the thread priorities. Can anyone help me to get the information about the thread priorities? If you have any program which will help me know more about thread priorities, then give it to me.
Hi, I want to know about the thread priorities. Can anyone help me to get the information about the thread priorities? If you have any program which will help me know more about thread priorities, then give it to me.
Hi, when you create any program with different thread, then not all threads are created equal. Some of the threads you want to give with more time than other. Threads that calculate in the background should get low priorities.
This can be achieved with the help of priorities. As per your requirement you can use java.lang.Thread which defines three mnemonic constants, Thread.MAX_PRIORITY, Thread.MIN_PRIORITY and Thread.NORM_PRIORITY while using thread. Program below will be helpful to you to get more knowledge about it. Look at it.
Code:public class Test { public static void main(String[] args) { NamedBytePrinter I = new NamedBytePrinter("I"); NamedBytePrinter WE = new NamedBytePrinter("WE"); NamedBytePrinter YOU = new NamedBytePrinter("YOU"); I.setPriority(Thread.MIN_PRIORITY); WE.setPriority(Thread.NORM_PRIORITY); YOU.setPriority(Thread.MAX_PRIORITY); I.start(); WE.start(); YOU.start(); } }
Hi, I am not having more knowledge about the threads in java. But you can use the following constants to give priorities in java. Just make use of it in your program.
- Constant - Description
- Thread.MIN_PRIORITY - The maximum priority of any thread (an int value of 10)
- Thread.MAX_PRIORITY - The minimum priority of any thread (an int value of 1)
- Thread.NORM_PRIORITY - The normal priority of any thread (an int value of 5)
HI, you can use following code to know how the threads priorities work in java.
JVM provides following constants for priority:
java.lang.Thread.MIN_PRIORITY = 1
java.lang.Thread.NORM_PRIORITY = 5
java.lang.Thread.MAX_PRIORITY = 10
Code:class thread1 extends Thread { thread1(String s) { super(s); start(); } public void run() { for(int i=0;i<3;i++) { Thread current=Thread.currentThread(); current.setPriority(Thread.MIN_PRIORITY); int p=current.getPriority(); System.out.println("Thread Name :"+Thread.currentThread().getName()); System.out.println("Thread Priority :"+current); } } } class thread2 extends Thread { thread2(String s) { super(s); start(); } public void run() { for(int i=0;i<3;i++) { Thread current=Thread.currentThread(); current.setPriority(Thread.MAX_PRIORITY); int p=current.getPriority(); System.out.println("Thread Name :"+Thread.currentThread().getName()); System.out.println("Thread Priority :"+current); } } } public class ThreadPriority { public static void main(String args[]) { thread1 m1=new thread1("My Thread 1"); thread2 m2=new thread2("My Thread 2"); } }
Bookmarks