Threads in Java Part 2
In the previous blog we learned about the Thread, the life cycle of thread, the creation of thread, etc. If you want to learn more about Threads, visit Threads in Java. In this, we will go through the basic concept of thread, multithreading, etc.
Thread Properties
- Interrupting Thread
- Daemon Thread
- Handlers for Uncaught Exceptions
- Thread Priorities
- MIN_PRIORITY - MIN_PRIORITY has a low priority with the value of 1.
- NORM_PRIORITY - NORM_PRIORITY has the default priority with the value of 5. If the programmer doesn’t set the priority explicitly then the JVM automatically set the default priority to the thread.
- MAX_PRIORITY - MAX_PRIORITY has the highest priority with a value of 10.
When the thread is waiting or sleeping state and if the interrupt() method is called on the thread, then it breaks out the sleeping or waiting state and throws the exception called InterruptedException. However, if the thread is not in a waiting or sleeping state and the interrupt() method is called, it will behave normally and will not interrupt, but it sets the interrupt flag to true. Now we will see the different scenarios where the thread can be interrupted.
Scenario 1 - Interrupting the thread that does not stop working - In this scenario, the InterruptedException will be handled using try and catch block due to which the thread will not stop working.
e.g.
Output
Scenario 2 - Interrupting the thread that stops working - In this scenario the thread will stop working when the new exception will be thrown.
e.g
Output
Daemon Thread runs as a low-priority thread and is a background service thread that performs background operations like garbage collection, etc. JVM exists only when all the threads running are demon threads.
setDaemon() - setDaemon() method is used to set the particular thread as a daemon thread or user thread. This method should be before the start of the thread.
e.g
Output
We have already learned about the exception in our previous blogs. So the exception is nothing but the unwanted event that occurs during the execution of the program at run time, due to which the normal flow gets affected. The run() method cannot throw a checked exception but due to an unchecked exception, it can get terminated. As the thread gets terminated it dies. Here no catch block is available instead exception is passed to the handler for uncaught exceptions just before the termination of thread. The handler which will be used must belong the class that implements the Thread.UncaughtExceptionHandler interface. The Thread.UncaughtExceptionHandler interface has the method called setDefaultUncaughtExceptionHandler() method. The argument of setDefaultUncaughtExceptionHandler() method is used to override the way JVM handles the uncaught exceptions.
e.g
Output
Priority is nothing but the importance given to a particular thing. The same concept is applicable in Java too. In Java, each thread has a priority. The priorities of the thread are set between 1 to 10. We can increase and decrease the priority of the thread by using the setPriority() method. The thread having a priority of 1 is considered less important and a priority of 10 is considered the most important. There are three constants used for defining the priorities.
e.g
Output