1. User thread
2. Daemon thread
Read Also: Difference between User and Daemon Thread in Java
Let's dive deep into each type of thread in Java.
Types of Threads in Java
1. User threads
User threads are high-priority threads that are created by the users or application. JVM will not exit until any user thread finishes its task before terminating it.
2. Daemon threads
Daemon threads are low-priority threads that are created to provide services to user threads. Daemon threads are only needed while user threads are running and are meant to serve them. Unlike user threads, daemon threads won't prevent the JVM from exiting once all user threads have finished their execution.
Daemon threads are used to perform background tasks such as garbage collection, removing unwanted entries from the cache, and releasing the memory of unused objects. JVM threads are mostly daemon threads.
Points to Remember about User thread and Daemon thread
1. You can convert a user thread to a daemon thread using setDaemon(true) method as shown below in the example.
public class DaemonThread {
// Main thread
public static void main(String args[]) {
// Creating UserThread object
UserThread userThread = new UserThread();
// Changing it to Daemon thread
userThread.setDaemon(true);
// Starting the thread
userThread.start();
}
}
class UserThread extends Thread {
@Override
public void run()
{
System.out.println("Inside UserThread run method");
}
}
Output:
Inside UserThread run method.
2. You can only call the setDaemon() method after the Thread object has been created and the thread has not been started. If you try to call setDaemon() method while a Thread is running then it will throw an IllegalThreadStateException as shown below in the example.
public class DaemonThread {
// Main thread
public static void main(String args[]) {
// Creating UserThread object
UserThread userThread = new UserThread();
// Starting the thread
userThread.start();
// Below line will throw IllegalThreadStateException
userThread.setDaemon(true);
}
}
class UserThread extends Thread {
@Override
public void run()
{
System.out.println("Inside UserThread run method. ");
}
}
Output:
Inside UserThread run method.
Exception in thread "main" java.lang.IllegalThreadStateException
at java.base/java.lang.Thread.setDaemon(Thread.java:1406)
at DaemonThread.main(DaemonThread.java:9)
3. isDaemon() method can be used to check if a thread is a daemon thread or a user thread. It returns boolean value, i.e. true if the current thread is daemon otherwise false as shown below in the example.
public class DaemonThread {
// Main thread
public static void main(String args[]) {
// Creating UserThread object
UserThread userThread = new UserThread();
System.out.println(userThread.isDaemon());// Output: false
// Changing it to Daemon thread
userThread.setDaemon(true);
// Starting the thread
userThread.start();
System.out.println(userThread.isDaemon());// Output: true
}
}
class UserThread extends Thread {
@Override
public void run()
{
System.out.println("Inside UserThread run method");
}
}
Output:
false
true
Inside UserThread run method
4. A thread daemon's property is inherited from its parent thread. In simple words, a thread created by a daemon thread will be a daemon thread and the thread created by a user thread will be a user thread as shown below in the example.
public class DaemonThread {
// Main thread
public static void main(String args[]) {
// Creating UserThread object
UserThread userThread = new UserThread();
// Changing it to Daemon thread
userThread.setDaemon(true);
// Starting the thread
userThread.start();
}
}
class UserThread extends Thread {
@Override
public void run()
{
// Creating child thread
Thread childThread = new Thread();
System.out.println(childThread.isDaemon()); // Output: true
}
}
Output:
true
5. Main thread i.e. thread executing public static void main(String args[]) method created by JVM is a user thread.
That's all for today. Please mention in the comments if you have any questions related to different types of threads in Java with examples.