Daemon Thread in JavaDaemon thread in Java is a service provider thread that provides services to the user thread. Its life depend on the mercy of user threads i.e. when all the user threads dies, JVM terminates this thread automatically. There are many java daemon threads running automatically e.g. gc, finalizer etc. You can see all the detail by typing the jconsole in the command prompt. The jconsole tool provides information about the loaded classes, memory usage, running threads etc. Points to remember for Daemon Thread in Java
Why JVM terminates the daemon thread if there is no user thread?The sole purpose of the daemon thread is that it provides services to user thread for background supporting task. If there is no user thread, why should JVM keep running this thread. That is why JVM terminates the daemon thread if there is no user thread. Methods for Java Daemon thread by Thread classThe java.lang.Thread class provides two methods for java daemon thread.
Simple example of Daemon thread in javaFile: MyThread.java Test it NowOutput: daemon thread work user thread work user thread work Note: If you want to make a user thread as Daemon, it must not be started otherwise it will throw IllegalThreadStateException.File: MyThread.java Test it NowOutput: exception in thread main: java.lang.IllegalThreadStateException Next TopicJava Thread Pool |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India