Thread Priority
Threads are assigned priorities that the thread scheduler can use to determine how the threads will be scheduled. The thread scheduler can use thread priorities to determine which thread gets to run. The thread scheduler favors giving CPU time to the thread with the highest priority in the Ready-to-run state. This is not necessarily the thread that has been the longest time in the Ready-to-run state. Heavy reliance on thread priorities for the behavior of a program can make the program unportable across platforms, as thread scheduling is host platform-dependent.
Priorities are integer values from 1 (lowest priority given by the constant Thread.MIN_PRIORITY) to 10 (highest priority given by the constant Thread.MAX_PRIORITY). The default priority is 5 (Thread.NORM_PRIORITY).
A thread inherits the priority of its parent thread. Priority of a thread can be set using the setPriority() method and read using the getPriority() method, both of which are defined in the Thread class. The following code sets the priority of the thread myThread to the minimum of two values: maximum priority and current priority incremented to the next level:
myThread.setPriority(Math.min( Thread.MAX_PRIORITY, myThread.getPriority()+1));.
Daemon Thread
Daemon thread in Java are those thread which runs in background and mostly created by JVM for performing background task like Garbage collection and other house keeping tasks.
Thread.setDaemon(true) makes a Thread daemon but it can only be called before starting Thread in Java. It will throw IllegalThreadStateException if corresponding Thread is already started and running.
As soon as last user thread finished JVM terminates no matter how many Daemon thread exists or running inside JVM.
ThreadGroup
- The thread group represents a set of threads.
- A thread group can also include other thread groups.
- The thread groups form a tree in which every thread group except the initial thread group has a parent.
- A thread from Thread group is allowed to access information about its own thread group, but not to access information about its thread group's parent thread group or any other thread groups.
ShutdownHookup
Java JVM provides you a hook to register a thread with the shutdown initiation sequence. Once a thread is registered, on every shutdown that thread is run.
JVM's shutdown sequence has two phases.
- All registered shutdown hooks are started in some unspecified order and allowed to run concurrently until they finish.
- All un-invoked finalizers are run if finalization-on-exit has been enabled.
System.getRuntime().addShutdownHook();
Example of ShutdownHookup
public class ShutdownHook
{
public static void main(String[] args)
{
Hook hook = new Hook();
System.out.println( "Running Main Application..." );
Runtime.getRuntime().addShutdownHook( hook );
System.out.println( "Exiting." );
}
private static class Hook extends Thread
{
public void run()
{
System.out.println( "Running Clean Up..." );
}
}
}
Anonymous class use in Thread
Example of Anonymous class use in thread:
Thread thread = new Thread(new Runnable()
{
@Override
public void run()
{
for(int i=0; i<10; i++)
{
System.out.println(i + " looping ...");
}
}
});
thread.start();
The above example shows the anonymous class use in thread, in above example What is the class of the object that we've passed to Thread() exactly? It's not an instance of
Runnable, Runnable is just an interface. It's an instance of a class that has no name(an anonymous class) that implements Runnable. We can use the same with abstract classes, and we can also override methods of existing classes in the same way.


