In Java multithreading programming, sometimes you may need to set Thread priority
in order for it to execute before another thread. You can set and get Thread priority programmatically using Thread.setPriority(5)
and Thread.getPriority()
methods.
Here are some basics about Thread priority in Java:
- What is default thread priority? It’s 5.
- What is a
MAX_PRIORITY
number? It’s 10. - What is
MIN_PRIORITY
NUMBER? It’s 1.
Let’s get started on our tutorial:
- We are going to create class CrunchifyJavaThreadPriority.java.
- Next we will create two child thread and will print its priority. Result would be 5.
- Next we will set priority to different number and will print the same.
- Next task is to print default Main() thread’s priority, print it, change it again and print it.
- On Next section, we will take a look at all other Thread Operations.
- isAlive(), isDaemon(), getStackTrace(), isInterrupted(), etc.
Kindly take a look at all comments in program for more explanation.
CrunchifyJavaThreadPriority.java
package crunchify.com.tutorial; /** * @author Crunchify.com * Program: Simplest way to Set and Get Thread Priority. Also get Thread ID, Count, Class, StackTrace, ThreadGroup and More. * Java Version: 1.0.0 * */ public class CrunchifyJavaThreadPriority extends Thread { @SuppressWarnings("static-access") public static void main(String[] args) { CrunchifyJavaThreadPriority crunchThread1 = new CrunchifyJavaThreadPriority(); CrunchifyJavaThreadPriority crunchThread2 = new CrunchifyJavaThreadPriority(); println("crunchThread1 thread default priority : " + crunchThread1.getPriority()); println("crunchThread2 thread default priority : " + crunchThread2.getPriority()); // We are changing Priorities for threads crunchThread1.setPriority(7); crunchThread2.setPriority(9); println("\ncrunchThread1 thread updated priority : " + crunchThread1.getPriority()); println("crunchThread2 thread updated priority : " + crunchThread2.getPriority()); println("\nCurrent Thread Name: " + Thread.currentThread().getName()); println("Main thread default priority : " + Thread.currentThread().getPriority()); Thread.currentThread().setPriority(2); println("Main thread new priority : " + Thread.currentThread().getPriority()); // Creating Child Thread.. Child Thread gets same priority as parent thread CrunchifyJavaThreadPriority crunchChildThread = new CrunchifyJavaThreadPriority(); println("Child thread priority same as Main thread: " + crunchChildThread.getPriority()); // More Thread Operations println("\n================= Let's work on all Thread Operations ================="); // Returns the identifier of this Thread. println("- getId() : " + crunchChildThread.getId()); // Returns an estimate of the number of active threads in the current thread's thread group and its subgroups. println("- activeCount() : " + crunchChildThread.activeCount()); // Returns the runtime class of this Object. println("- getClass() : " + crunchChildThread.getClass()); // Returns the context ClassLoader for this Thread. println("- getContextClassLoader() : " + crunchChildThread.getContextClassLoader()); // Returns an array of stack trace elements representing the stack dump of this thread. println("- getStackTrace() : " + crunchChildThread.getStackTrace()); // Returns the state of this thread. println("- getState() : " + crunchChildThread.getState()); // Returns the thread group to which this thread belongs. println("- getThreadGroup() : " + crunchChildThread.getThreadGroup()); // Returns a map of stack traces for all live threads. println("- getAllStackTraces() : " + crunchChildThread.getAllStackTraces()); // True if this thread is alive. println("- isAlive() : " + crunchChildThread.isAlive()); // True if thread is a daemon thread println("- isDaemon() : " + crunchChildThread.isDaemon()); // Tests whether this thread has been interrupted. println("- isInterrupted() : " + crunchChildThread.isInterrupted()); } @Override public void run() { println("Inside run method"); } private static void println(String string) { System.out.println(string); } }
Run above program in Eclipse and you should result similar to below:
crunchThread1 thread default priority : 5 crunchThread2 thread default priority : 5 crunchThread1 thread updated priority : 7 crunchThread2 thread updated priority : 9 Current Thread Name: main Main thread default priority : 5 Main thread new priority : 2 Child thread priority same as Main thread: 2 ================= Let's work on all Thread Operations ================= - getId() : 11 - activeCount() : 1 - getClass() : class crunchify.com.tutorial.CrunchifyJavaThreadPriority - getContextClassLoader() : sun.misc.Launcher$AppClassLoader@4554617c - getStackTrace() : [Ljava.lang.StackTraceElement;@677327b6 - getState() : NEW - getThreadGroup() : java.lang.ThreadGroup[name=main,maxpri=10] - getAllStackTraces() : {Thread[Finalizer,8,system]=[Ljava.lang.StackTraceElement;@45ee12a7, Thread[main,2,main]=[Ljava.lang.StackTraceElement;@330bedb4, Thread[Signal Dispatcher,9,system]=[Ljava.lang.StackTraceElement;@2503dbd3, Thread[Reference Handler,10,system]=[Ljava.lang.StackTraceElement;@4b67cf4d} - isAlive() : false - isDaemon() : false - isInterrupted() : false
I hope you get detailed information on how to set and get Thread priority. Also, let us know if you see any issue in multithreading programming, threadsafe singleton class, etc
The post In Java How to Set and Get Thread Priority? Get Thread ID, Count, Class, StackTrace, ThreadGroup and More appeared first on Crunchify.
0 Commentaires