Related
I just stumbled upon a weird behavior of daemon threads which I can't explain. I've reduced my code to a minimal, complete and verifiable sample:
public static void main(String[] args) throws InterruptedException {
Thread runner = new Thread(() -> {
final int SIZE = 350_000;
for (int i = 0; i < SIZE; i++) {
for (int j = i + 1; j < SIZE; j++) {
if (i*j == SIZE * SIZE - 1) {
return;
}
}
}
});
runner.setDaemon(true);
runner.start();
// Thread.sleep(1000);
System.out.println("Exiting.");
}
The code executed by the runner thread takes about 12 secs to terminate on my box, and we're not interested in what it does, since I just needed to spend some time computing.
If this snippet is run as it is, it works as expected: it terminates just after its start.
If I uncomment the Thread.sleep(1000) line and run the program, it works for about 12 seconds, then prints out "Exiting" and terminates.
As far as I understood how daemon threads work, I expected this code to run for 1 second and then to terminate execution, since the only user thread running is the one launched with the main() method (the runner is a background daemon thread) and as soon as the 1000 msec are passed, it reaches the end of its execution and the JVM should stop. Also, it looks quite strange that "Exiting" is printed only after 12 seconds, and not when the program starts.
Am I wrong? How can I achieve the desired behavior (pause for a second and then stop, independently from what the runner thread is doing)?
I'm using a 64bit Oracle JDK 1.8.0_112 on a linux box and it has the same behavior either if launched from an IDE or from the command line.
Thanks,
Andrea
This is maybe a consequence of counted loop optimization which removed safepoint polls from your nested loops. Try to add -XX:+UseCountedLoopSafepoint flag to your JVM startup options.
Thread#sleep(long) pauses the main thread before it returns from its main method (i.e. before the JVM is considering the program done as long as no non-deamon threads are alive). The scheduler is then free to run any other runnable thread which would be the deamon thread. As it stands, there is no apparent reason for the JVM to forcibly preempt the deamon thread before it finishes execution to continue in the main thread (is it's done sleeping yet), so the JVM is free to continue its schedule. However, it may at any time elect to pause the running thread and schedule another runnable thread for execution, so reproducibility is not guaranteed for your example.
You can force a preemption by inserting calls to Thread#yield() or #sleep(1) in the loops. I bet you'll start seeing the snippet exiting faster and before it finishes the loops.
There's more to learn about Thread states and scheduling, a nice overview can be found here.
Update for comment:
I cannot modify the code in the background thread (is a requirement), so I was looking for a way to stop it if it takes too long (a description of what I'm doing is stackoverflow.com/questions/41226054/… ).
It's legally only possible to stop a running thread from within, so you usually have it test an abort condition every iteration, and if the condition is met, the run method return;s. An abort condition could be as simple as a boolean flag that is set from the outside (! volatile caveat !). So the dead-simplest solution would be to have the main thread set such a flag after the sleep.
Another possibility might be using an ExecutorService that supports timeouts, see this Q&A for an example involving ScheduledExecutorService.
Still I don't understand how the scheduler can decide to wait for 12 seconds before running the System.out instruction.
It does not wait 12 seconds, it let's the deamon thread run to completion because being a deamon only matters to the JVM when deciding if it's safe to halt the JVM. For the scheduler, only the state of the thread matters and as far as it's concernced, after the 1s sleep of the main thread, it has a running (deamon) and a runnable thread (main), and no indication that the running thread should be paused in favor for the runnable thread. Switching threads is also expensive computationally, so the scheduler might be reluctant lacking any indication. An indication to switch might be sleeps and yields, but also GC runs and a whole lot of other things.
Can anybody tell me what daemon threads are in Java?
A daemon thread is a thread that does not prevent the JVM from exiting when the program finishes but the thread is still running. An example for a daemon thread is the garbage collection.
You can use the setDaemon(boolean) method to change the Thread daemon properties before the thread starts.
A few more points (Reference: Java Concurrency in Practice)
When a new thread is created it inherits the daemon status of its
parent.
When all non-daemon threads finish, the JVM halts, and any remaining daemon threads are abandoned:
finally blocks are not executed,
stacks are not unwound - the JVM just exits.
Due to this reason daemon threads should be used sparingly, and it is dangerous to use them for tasks that might perform any sort of I/O.
All the above answers are good. Here's a simple little code snippet, to illustrate the difference. Try it with each of the values of true and false in setDaemon.
public class DaemonTest {
public static void main(String[] args) {
new WorkerThread().start();
try {
Thread.sleep(7500);
} catch (InterruptedException e) {
// handle here exception
}
System.out.println("Main Thread ending") ;
}
}
class WorkerThread extends Thread {
public WorkerThread() {
// When false, (i.e. when it's a non daemon thread),
// the WorkerThread continues to run.
// When true, (i.e. when it's a daemon thread),
// the WorkerThread terminates when the main
// thread or/and user defined thread(non daemon) terminates.
setDaemon(true);
}
public void run() {
int count = 0;
while (true) {
System.out.println("Hello from Worker "+count++);
try {
sleep(5000);
} catch (InterruptedException e) {
// handle exception here
}
}
}
}
Traditionally daemon processes in UNIX were those that were constantly running in background, much like services in Windows.
A daemon thread in Java is one that doesn't prevent the JVM from exiting. Specifically the JVM will exit when only daemon threads remain. You create one by calling the setDaemon() method on Thread.
Have a read of Daemon threads.
Daemon threads are like a service providers for other threads or objects running in the same process as the daemon thread. Daemon threads are used for background supporting tasks and are only needed while normal threads are executing. If normal threads are not running and remaining threads are daemon threads then the interpreter exits.
For example, the HotJava browser uses up to four daemon threads named "Image Fetcher" to fetch images from the file system or network for any thread that needs one.
Daemon threads are typically used to perform services for your application/applet (such as loading the "fiddley bits"). The core difference between user threads and daemon threads is that the JVM will only shut down a program when all user threads have terminated. Daemon threads are terminated by the JVM when there are no longer any user threads running, including the main thread of execution.
setDaemon(true/false) ? This method is used to specify that a thread is daemon thread.
public boolean isDaemon() ? This method is used to determine the thread is daemon thread or not.
Eg:
public class DaemonThread extends Thread {
public void run() {
System.out.println("Entering run method");
try {
System.out.println("In run Method: currentThread() is" + Thread.currentThread());
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException x) {}
System.out.println("In run method: woke up again");
}
} finally {
System.out.println("Leaving run Method");
}
}
public static void main(String[] args) {
System.out.println("Entering main Method");
DaemonThread t = new DaemonThread();
t.setDaemon(true);
t.start();
try {
Thread.sleep(3000);
} catch (InterruptedException x) {}
System.out.println("Leaving main method");
}
}
OutPut:
C:\java\thread>javac DaemonThread.java
C:\java\thread>java DaemonThread
Entering main Method
Entering run method
In run Method: currentThread() isThread[Thread-0,5,main]
In run method: woke up again
In run method: woke up again
In run method: woke up again
In run method: woke up again
In run method: woke up again
In run method: woke up again
Leaving main method
C:\j2se6\thread>
daemon: d(isk) a(nd) e(xecution) mon(itor) or from de(vice) mon(itor)
Definition of Daemon (Computing):
A background process that handles requests for services such as print spooling and file transfers, and is dormant when not required.
—— Source: English by Oxford Dictionaries
What is Daemon thread in Java?
Daemon threads can shut down any time in between their flow, Non-Daemon i.e. user thread executes completely.
Daemon threads are threads that run intermittently in the background as long as other non-daemon threads are running.
When all of the non-daemon threads complete, daemon threads terminates automatically.
Daemon threads are service providers for user threads running in the same process.
The JVM does not care about daemon threads to complete when in Running state, not even finally block also let execute. JVM do give preference to non-daemon threads that is created by us.
Daemon threads acts as services in Windows.
The JVM stops the daemon threads when all user threads (in contrast to the daemon threads) are terminated. Hence daemon threads can be used to implement, for example, a monitoring functionality as the thread is stopped by the JVM as soon as all user threads have stopped.
A daemon thread is a thread that is considered doing some tasks in the background like handling requests or various chronjobs that can exist in an application.
When your program only have daemon threads remaining it will exit. That's because usually these threads work together with normal threads and provide background handling of events.
You can specify that a Thread is a daemon one by using setDaemon method, they usually don't exit, neither they are interrupted.. they just stop when application stops.
One misconception I would like to clarify:
Assume that if daemon thread (say B) is created within user thread (say
A); then ending of this user thread/parent thread (A) will not end
the daemon thread/child thread (B) it has created; provided user thread is the only
one currently running.
So there is no parent-child relationship on thread ending. All daemon threads (irrespective of where it is created) will end once there is no single live user thread and that causes JVM to terminate.
Even this is true for both (parent/child) are daemon threads.
If a child thread created from a daemon thread then that is also a daemon thread. This won't need any explicit daemon thread flag setting.
Similarly if a child thread created from a user thread then that is also a user thread, if you want to change it, then explicit daemon flag setting is needed before start of that child thread.
Daemon Thread and User Threads. Generally all threads created by programmer are user thread (unless you specify it to be daemon or your parent thread is a daemon thread). User thread are generally meant to run our programm code. JVM doesn't terminates unless all the user thread terminate.
Java has a special kind of thread called daemon thread.
Very low priority.
Only executes when no other thread of the same program is running.
JVM ends the program finishing these threads, when daemon threads are
the only threads running in a program.
What are daemon threads used for?
Normally used as service providers for normal threads.
Usually have an infinite loop that waits for the service request or performs the tasks of the thread.
They can’t do important jobs. (Because we don't know when they are going to have CPU time and they can finish any time if there aren't any other threads running. )
A typical example of these kind of threads is the Java garbage collector.
There's more...
You only call the setDaemon() method before you call the start() method. Once the thread is running, you can’t modify its daemon status.
Use isDaemon() method to check if a thread is a daemon thread or a user thread.
In Java, Daemon Threads are one of the types of the thread which does not prevent Java Virtual Machine (JVM) from exiting.
The main purpose of a daemon thread is to execute background task especially in case of some routine periodic task or work. With JVM exits, daemon thread also dies.
By setting a thread.setDaemon(true), a thread becomes a daemon thread. However, you can only set this value before the thread start.
Daemon threads are like assistants. Non-Daemon threads are like front performers. Assistants help performers to complete a job. When the job is completed, no help is needed by performers to perform anymore. As no help is needed the assistants leave the place. So when the jobs of Non-Daemon threads is over, Daemon threads march away.
Here is an example to test behavior of daemon threads in case of jvm exit due to non existence of user threads.
Please note second last line in the output below, when main thread exited, daemon thread also died and did not print finally executed9 statement within finally block. This means that any i/o resources closed within finally block of a daemon thread will not be closed if JVM exits due to non existence of user threads.
public class DeamonTreadExample {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
int count = 0;
while (true) {
count++;
try {
System.out.println("inside try"+ count);
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
System.out.println("finally executed"+ count);
}
}
});
t.setDaemon(true);
t.start();
Thread.currentThread().sleep(10000);
System.out.println("main thread exited");
}
}
Output
inside try1
finally executed1
inside try2
finally executed2
inside try3
finally executed3
inside try4
finally executed4
inside try5
finally executed5
inside try6
finally executed6
inside try7
finally executed7
inside try8
finally executed8
inside try9
finally executed9
inside try10
main thread exited
Daemon thread is just like a normal thread except that the JVM will only shut down when the other non daemon threads are not existing. Daemon threads are typically used to perform services for your application.
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.
Points to Note :
Any thread created by main thread, which runs main method in Java is by default non daemon because Thread inherits its daemon nature from the Thread which creates it i.e. parent Thread and since main thread is a non daemon thread, any other thread created from it will remain non-daemon until explicitly made daemon by calling setDaemon(true).
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.
Difference between Daemon and Non Daemon thread in Java :
1) JVM doesn't wait for any daemon thread to finish before existing.
2) Daemon Thread are treated differently than User Thread when JVM terminates, finally blocks are not called, Stacks are not unwounded and JVM just exits.
Daemon threads are as everybody explained, will not constrain JVM to exit, so basically its a happy thread for Application from exit point of view.
Want to add that daemon threads can be used when say I'm providing an API like pushing data to a 3rd party server / or JMS, I might need to aggregate data at the client JVM level and then send to JMS in a separate thread. I can make this thread as daemon thread, if this is not a mandatory data to be pushed to server.
This kind of data is like log push / aggregation.
Regards,
Manish
Daemon thread is like daemon process which is responsible for managing resources,a daemon thread is created by the Java VM to serve the user threads.
example updating system for unix,unix is daemon process.
child of daemon thread is always daemon thread,so by default daemon is false.you can check thread as daemon or user by using "isDaemon()" method.
so daemon thread or daemon process are basically responsible for managing resources.
for example when you starting jvm there is garbage collector running that is daemon thread whose priority is 1 that is lowest,which is managing memory.
jvm is alive as long as user thread is alive,u can not kill daemon thread.jvm is responsible to kill daemon threads.
For me, daemon thread it's like house keeper for user threads.
If all user threads finished , the daemon thread has no job and
killed by JVM.
I explained it in the YouTube video.
Let's talk only in code with working examples. I like russ's answer above but to remove any doubt I had, I enhanced it a little bit. I ran it twice, once with the worker thread set to deamon true (deamon thread) and another time set it to false (user thread). It confirms that the deamon thread ends when the main thread terminates.
public class DeamonThreadTest {
public static void main(String[] args) {
new WorkerThread(false).start(); //set it to true and false and run twice.
try {
Thread.sleep(7500);
} catch (InterruptedException e) {
// handle here exception
}
System.out.println("Main Thread ending");
}
}
class WorkerThread extends Thread {
boolean isDeamon;
public WorkerThread(boolean isDeamon) {
// When false, (i.e. when it's a user thread),
// the Worker thread continues to run.
// When true, (i.e. when it's a daemon thread),
// the Worker thread terminates when the main
// thread terminates.
this.isDeamon = isDeamon;
setDaemon(isDeamon);
}
public void run() {
System.out.println("I am a " + (isDeamon ? "Deamon Thread" : "User Thread (none-deamon)"));
int counter = 0;
while (counter < 10) {
counter++;
System.out.println("\tworking from Worker thread " + counter++);
try {
sleep(5000);
} catch (InterruptedException e) {
// handle exception here
}
}
System.out.println("\tWorker thread ends. ");
}
}
result when setDeamon(true)
=====================================
I am a Deamon Thread
working from Worker thread 0
working from Worker thread 1
Main Thread ending
Process finished with exit code 0
result when setDeamon(false)
=====================================
I am a User Thread (none-deamon)
working from Worker thread 0
working from Worker thread 1
Main Thread ending
working from Worker thread 2
working from Worker thread 3
working from Worker thread 4
working from Worker thread 5
working from Worker thread 6
working from Worker thread 7
working from Worker thread 8
working from Worker thread 9
Worker thread ends.
Process finished with exit code 0
Daemon threads are generally known as "Service Provider" thread. These threads should not be used to execute program code but system code. These threads run parallel to your code but JVM can kill them anytime. When JVM finds no user threads, it stops it and all daemon threads terminate instantly. We can set non-daemon thread to daemon using :
setDaemon(true)
Daemon threads are threads that run in the background as long as other non-daemon threads of the process are still running. Thus, when all of the non-daemon threads complete, the daemon threads are terminated. An example for the non-daemon thread is the thread running the Main.
A thread is made daemon by calling the setDaemon() method before the thread is started
For More Reference : Daemon thread in Java
There already are numerous answers; however, maybe I could shed a bit clearer light on this, as when I was reading about Daemon Threads, initially, I had a feeling, that I understood it well; however, after playing with it and debugged a bit, I observed a strange (to me) behaviour.
I was taught, that:
If I want the thread to die right after the main thread orderly finishes its execution, I should set it as Diamond.
What I tried:
I created two threads from the Main Thread, and I only set one of those as a diamond;
After orderly completing execution of the Main Thread, none of those newly created threads exited, but I expected, that Daemon thread should have been exited;
I surfed over many blogs and articles, and the best and clearest definition I found so far, comes from the Java Concurrency In Practice book, which very clearly states, that:
7.4.2 Daemon threads
Sometimes you want to create a thread that performs some helper
function but you don’t want the existence of this thread to prevent
the JVM from shutting down. This is what daemon threads are for.
Threads are divided into two types: normal threads and daemon threads.
When the JVM starts up, all the threads it creates (such as garbage
collector and other housekeeping threads) are daemon threads, except
the main thread. When a new thread is created, it inherits the daemon
status of the thread that created it, so by default any threads
created by the main thread are also normal threads. Normal threads and
daemon threads differ only in what happens when they exit. When a
thread exits, the JVM performs an inventory of running threads, and if
the only threads that are left are daemon threads, it initiates an
orderly shutdown. When the JVM halts, any remaining daemon threads are
abandoned— finally blocks are not executed, stacks are not unwound—the
JVM just exits. Daemon threads should be used sparingly—few processing
activities can be safely abandoned at any time with no cleanup. In
particular, it is dangerous to use daemon threads for tasks that might
perform any sort of I/O. Daemon threads are best saved for
“housekeeping” tasks, such as a background thread that periodically
removes expired entries from an in-memory cache.
JVM will accomplish the work when a last non-daemon thread execution is completed. By default, JVM will create a thread as nondaemon but we can make Thread as a daemon with help of method setDaemon(true). A good example of Daemon thread is GC thread which will complete his work as soon as all nondaemon threads are completed.
Daemon threads are those threads which provide general services for user threads (Example : clean up services - garbage collector)
Daemon threads are running all the time until kill by the JVM
Daemon Threads are treated differently than User Thread when JVM terminates , finally blocks are not called JVM just exits
JVM doesn't terminates unless all the user threads terminate. JVM terminates if all user threads are dies
JVM doesn't wait for any daemon thread to finish before existing and finally blocks are not called
If all user threads dies JVM kills all the daemon threads before stops
When all user threads have terminated, daemon threads can also be terminated and the main program terminates
setDaemon() method must be called before the thread's start() method is invoked
Once a thread has started executing its daemon status cannot be changed
To determine if a thread is a daemon thread, use the accessor method isDaemon()
Java daemon thread
[Daemon process]
Java uses user thread and daemon tread concepts.
JVM flow
1. If there are no `user treads` JVM starts terminating the program
2. JVM terminates all `daemon threads` automatically without waiting when they are done
3. JVM is shutdown
As you see daemon tread is a service thread for user treads.
daemon tread is low priority thread.
Thread inherits it's properties from parent thread. To set it externally you can use setDaemon() method before starting it or check it via isDaemon()
Daemon Thread
Threads that run in the background are called daemon threads.
Example of Daemon Threads:
Garbage Collector.
Signal Dispatcher.
Objective of Daemon Thread:
The main objective of the daemon threads is to provide support to the non-daemon threads.
Additional information about Daemon Thread:
Generally, Daemon threads run in the MIN_PRIORITY however, it is possible to run daemon threads with MAX_PRIORITY as well.
Example: Usually the GC runs with a MIN_PRIORITY priority, however, once there is a requirement for additional memory. JVM increases the priority of the GC from MIN_PRIORITY to MAX_PRIORITY.
Once the Thread has been started, it can't be changed from Daemon Thread to Non-Daemon Thread that will result in IllegalThreadStateException.
Example:
public static void main(String[] args) {
Thread.currentThread().setDaemon(true);
}
Output:
Exception in thread "main" java.lang.IllegalThreadStateException
at java.base/java.lang.Thread.setDaemon(Thread.java:1403)
If we are branching off a thread, the child thread inherits the nature of the parent thread. If the parent thread is a non-daemon thread automatically the child thread will be non-daemon as well and if the parent thread is a daemon, the child thread will be a daemon as well.
class Scratch {
public static void main(String[] args) {
CustomThread customThread = new CustomThread();
customThread.start();
}
}
class CustomThread extends Thread{
#Override
public void run() {
System.out.println(currentThread().isDaemon());
}
}
Output:
false
When the last non-daemon thread terminates, all the daemon threads get terminated automatically.
class Scratch {
public static void main(String[] args) {
System.out.println("Main Thread Started.");
CustomThread customThread = new CustomThread();
customThread.setDaemon(true);
customThread.start();
System.out.println("Main Thread Finished.");
}
}
class CustomThread extends Thread{
#Override
public void run() {
System.out.println("Custom Thread Started.");
try {
sleep(2000);
} catch (InterruptedException ignore) {}
System.out.println("Custom Thread Finished."); //Won't get executed.
}
}
Output:
Main Thread Started.
Main Thread Finished.
Custom Thread Started.
User threads versus Daemon threads in java threads
Daemon Threads
this threads in Java are low-priority threads that runs in the background to perform tasks such as garbage collection. Daemon thread in Java is also a service provider thread that provides services to the user thread.
User Threads
this threads are high-priority threads. The JVM will wait for any user thread to complete its task before terminating it
"keep in mind both User and Daemon threads wrapped upon OS threads"
Recently OpenJdk proposed Virtual threads with in project Loom (which they are User based as well) you may find more on Fibers and Continuations for the Java Virtual Machine threads in here.
I am trying to understand multi-threading in Java. I have written the following java program to test thread pool.
public class ThreadPoolTest
{
public static void main(String[] args)
{
ExecutorService executorService = Executors.newFixedThreadPool(5);
for( int i = 0; i < 3; i++ )
{
executorService.submit(new Task(i+1));
}
executorService.shutdown();
}
public static class Task implements Runnable
{
private int taskId;
public Task(int id)
{
taskId = id;
}
#Override
public void run() {
System.out.println("Executing task " + taskId + " performed by " + Thread.currentThread().getName() );
try
{
Thread.sleep(3000);
}
catch(InterruptedException interruptEx)
{
System.out.println(Thread.currentThread().getName() + " got interrupted ");
}
System.out.println("Finished executing task " + taskId );
}
}
}
The main thread creates executor which creates 5 threads and I have submitted only 3 tasks. After that I am shutting down the executor. When I run the code, the main thread finishes before the child threads. In this case, does JVM takes care of the child threads?
Also I have created a thread pool with 5 threads, but submitted only 3 tasks. Will the remaining 2 threads are terminated when the main thread exits?
What actually happens when the executor service is shutdown?
From the doc of ExecutorService#shutdown():
Initiates an orderly shutdown in which previously submitted tasks are
executed, but no new tasks will be accepted.
This means that all the Jobs you submitted to the Executor will finish on their own time without interrupting or "hurrying" them, and the executor will finish up the worker threads properly, but neither will the service accept new Jobs, nor will it terminate instantly.
Compare ExecutorService#shutdownNow(), which will try to terminate as quickly as possible.
The Worker threads that the executor creates are inner classes that have a reference back to the executor itself. (They need it to be able to see the queue, runstate, etc!) Running threads are not garbage collected, so with each Thread in the pool having that reference, they will keep the executor alive until all threads are dead. If you don't manually do something to stop the threads, they will keep running forever and your JVM will never shut down.
In case of 5 threads where 3 tasks are only spawned, 2 unused thread will never be started but there references will remain as it is until shutdown is called in finalize() or all the live threads completes there execution.
Following is the comment from JAVA Docs:
A pool that is no longer referenced in a program AND has no remaining threads will be >shutdown automatically. If you would like to ensure that unreferenced pools are reclaimed >even if users forget to call shutdown(), then you must arrange that unused threads >eventually die, by setting appropriate keep-alive times, using a lower bound of zero core >threads and/or setting allowCoreThreadTimeOut(boolean).
In this case, does JVM takes care of the child threads?
The OS manages threads and determines when they are run.
Also I have created a thread pool with 5 threads, but submitted only 3 tasks. Will the remaining 2 threads are terminated when the main thread exits?
No, the threads run until you shut them down.
What actually happens when the executor service is shutdown?
The threads are interrupted and no new tasks will start. However, if you have a task which ignores interrupts it could keep running indefinitely.
When the last non-deamon thread stops the shutdown hook (if any) are triggered.
does JVM takes care of the child threads
JVM only complete its execution after completing all daemon threads. If you are creating your as non daemon then it will wait till all non daemon threads complete.
Will the remaining 2 threads are terminated when the main thread exits
Threads will be created in ondemand mode. So here 3 threads only created not 5.
What actually happens when the executor service is shutdown
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.
This method does not wait for previously submitted tasks to complete execution. Use awaitTermination to do that.
I have a ThreadPoolExecutor:
ThreadPoolExecutor service = new ThreadPoolExecutor(N_THREADS, N_THREADS, 0L, TimeUnit.MILLISECONDS, blockingQueue, rejectedExecutionHandler);
The service executes threads implementing the Runnable interface. Each thread processes a file on disk. I found that after several hours, two threads (or cores depending on what htop shows in Linux) were running and had been running for 13 hours. What's even worse is that the remaining cores showed no activity as if they were waiting for the two threads to complete.
Questions:
1 - I have read a lot on how this problem may be resolved but nothing conclusive. As far as I can work out, you CANNOT stop a Runnable using the ThreadPoolExecutor because it is an independent thread that just runs. Using the Future framework:
Future<?> f = f.get(submittedtask,XX)
allows you to set a timeout and fetch the future result, but get blocks all the threads effectively making the implementation serial. Is it possible to interrupt a Runnable after a given time using the threadpoolexecutor, get the thread back to the pool so it can pickup a new task and carry on.
2 - My big concern is why, using htop, I see two threads/cores running and no other core/thread are running despite many tasks are still waiting to execute (i.e. there are many files left to process). Any insight?
You could create a second scheduled thread pool to which you would submit cancellation tasks for each of the returned Futures. Each of these tasks after a given timeout would check if it's associated Future is done and if not, cancel it. Cancellation would trigger thread interruption, so you might need to support it in your tasks by checking the interrupted flag: Thread.interrupted().
The size of this second thread pool could be minimal, i.e. 1 as this job takes minimum of CPU time.
Code example:
ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
...
while(...){
final Future<?> f = pool.submit(...);
service.schedule(new Runnable() {
#Override
public void run() {
if(!f.isDone()){
f.cancel(true);
}
}
}, 1, TimeUnit.MINUTES);
}
service.awaitTermination(1, TimeUnit.MINUTES);
service.shutdown();
You can tell a thread that you wish to interrupt:
An interrupt is an indication to a thread that it should stop what it is doing and do something else.
You can interrupt your thread with Future.cancel(true). It's in your responsibility to implement the Runnable in a manner that it obeys that wish by checking its Thread.interrupted() state.
In order to see details about process thread run:
ps -eLf | grep <PROCESS_PID>
Since htop shows you the running processes list, where each process has at least one thread.
Can anybody tell me what daemon threads are in Java?
A daemon thread is a thread that does not prevent the JVM from exiting when the program finishes but the thread is still running. An example for a daemon thread is the garbage collection.
You can use the setDaemon(boolean) method to change the Thread daemon properties before the thread starts.
A few more points (Reference: Java Concurrency in Practice)
When a new thread is created it inherits the daemon status of its
parent.
When all non-daemon threads finish, the JVM halts, and any remaining daemon threads are abandoned:
finally blocks are not executed,
stacks are not unwound - the JVM just exits.
Due to this reason daemon threads should be used sparingly, and it is dangerous to use them for tasks that might perform any sort of I/O.
All the above answers are good. Here's a simple little code snippet, to illustrate the difference. Try it with each of the values of true and false in setDaemon.
public class DaemonTest {
public static void main(String[] args) {
new WorkerThread().start();
try {
Thread.sleep(7500);
} catch (InterruptedException e) {
// handle here exception
}
System.out.println("Main Thread ending") ;
}
}
class WorkerThread extends Thread {
public WorkerThread() {
// When false, (i.e. when it's a non daemon thread),
// the WorkerThread continues to run.
// When true, (i.e. when it's a daemon thread),
// the WorkerThread terminates when the main
// thread or/and user defined thread(non daemon) terminates.
setDaemon(true);
}
public void run() {
int count = 0;
while (true) {
System.out.println("Hello from Worker "+count++);
try {
sleep(5000);
} catch (InterruptedException e) {
// handle exception here
}
}
}
}
Traditionally daemon processes in UNIX were those that were constantly running in background, much like services in Windows.
A daemon thread in Java is one that doesn't prevent the JVM from exiting. Specifically the JVM will exit when only daemon threads remain. You create one by calling the setDaemon() method on Thread.
Have a read of Daemon threads.
Daemon threads are like a service providers for other threads or objects running in the same process as the daemon thread. Daemon threads are used for background supporting tasks and are only needed while normal threads are executing. If normal threads are not running and remaining threads are daemon threads then the interpreter exits.
For example, the HotJava browser uses up to four daemon threads named "Image Fetcher" to fetch images from the file system or network for any thread that needs one.
Daemon threads are typically used to perform services for your application/applet (such as loading the "fiddley bits"). The core difference between user threads and daemon threads is that the JVM will only shut down a program when all user threads have terminated. Daemon threads are terminated by the JVM when there are no longer any user threads running, including the main thread of execution.
setDaemon(true/false) ? This method is used to specify that a thread is daemon thread.
public boolean isDaemon() ? This method is used to determine the thread is daemon thread or not.
Eg:
public class DaemonThread extends Thread {
public void run() {
System.out.println("Entering run method");
try {
System.out.println("In run Method: currentThread() is" + Thread.currentThread());
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException x) {}
System.out.println("In run method: woke up again");
}
} finally {
System.out.println("Leaving run Method");
}
}
public static void main(String[] args) {
System.out.println("Entering main Method");
DaemonThread t = new DaemonThread();
t.setDaemon(true);
t.start();
try {
Thread.sleep(3000);
} catch (InterruptedException x) {}
System.out.println("Leaving main method");
}
}
OutPut:
C:\java\thread>javac DaemonThread.java
C:\java\thread>java DaemonThread
Entering main Method
Entering run method
In run Method: currentThread() isThread[Thread-0,5,main]
In run method: woke up again
In run method: woke up again
In run method: woke up again
In run method: woke up again
In run method: woke up again
In run method: woke up again
Leaving main method
C:\j2se6\thread>
daemon: d(isk) a(nd) e(xecution) mon(itor) or from de(vice) mon(itor)
Definition of Daemon (Computing):
A background process that handles requests for services such as print spooling and file transfers, and is dormant when not required.
—— Source: English by Oxford Dictionaries
What is Daemon thread in Java?
Daemon threads can shut down any time in between their flow, Non-Daemon i.e. user thread executes completely.
Daemon threads are threads that run intermittently in the background as long as other non-daemon threads are running.
When all of the non-daemon threads complete, daemon threads terminates automatically.
Daemon threads are service providers for user threads running in the same process.
The JVM does not care about daemon threads to complete when in Running state, not even finally block also let execute. JVM do give preference to non-daemon threads that is created by us.
Daemon threads acts as services in Windows.
The JVM stops the daemon threads when all user threads (in contrast to the daemon threads) are terminated. Hence daemon threads can be used to implement, for example, a monitoring functionality as the thread is stopped by the JVM as soon as all user threads have stopped.
A daemon thread is a thread that is considered doing some tasks in the background like handling requests or various chronjobs that can exist in an application.
When your program only have daemon threads remaining it will exit. That's because usually these threads work together with normal threads and provide background handling of events.
You can specify that a Thread is a daemon one by using setDaemon method, they usually don't exit, neither they are interrupted.. they just stop when application stops.
One misconception I would like to clarify:
Assume that if daemon thread (say B) is created within user thread (say
A); then ending of this user thread/parent thread (A) will not end
the daemon thread/child thread (B) it has created; provided user thread is the only
one currently running.
So there is no parent-child relationship on thread ending. All daemon threads (irrespective of where it is created) will end once there is no single live user thread and that causes JVM to terminate.
Even this is true for both (parent/child) are daemon threads.
If a child thread created from a daemon thread then that is also a daemon thread. This won't need any explicit daemon thread flag setting.
Similarly if a child thread created from a user thread then that is also a user thread, if you want to change it, then explicit daemon flag setting is needed before start of that child thread.
Daemon Thread and User Threads. Generally all threads created by programmer are user thread (unless you specify it to be daemon or your parent thread is a daemon thread). User thread are generally meant to run our programm code. JVM doesn't terminates unless all the user thread terminate.
Java has a special kind of thread called daemon thread.
Very low priority.
Only executes when no other thread of the same program is running.
JVM ends the program finishing these threads, when daemon threads are
the only threads running in a program.
What are daemon threads used for?
Normally used as service providers for normal threads.
Usually have an infinite loop that waits for the service request or performs the tasks of the thread.
They can’t do important jobs. (Because we don't know when they are going to have CPU time and they can finish any time if there aren't any other threads running. )
A typical example of these kind of threads is the Java garbage collector.
There's more...
You only call the setDaemon() method before you call the start() method. Once the thread is running, you can’t modify its daemon status.
Use isDaemon() method to check if a thread is a daemon thread or a user thread.
In Java, Daemon Threads are one of the types of the thread which does not prevent Java Virtual Machine (JVM) from exiting.
The main purpose of a daemon thread is to execute background task especially in case of some routine periodic task or work. With JVM exits, daemon thread also dies.
By setting a thread.setDaemon(true), a thread becomes a daemon thread. However, you can only set this value before the thread start.
Daemon threads are like assistants. Non-Daemon threads are like front performers. Assistants help performers to complete a job. When the job is completed, no help is needed by performers to perform anymore. As no help is needed the assistants leave the place. So when the jobs of Non-Daemon threads is over, Daemon threads march away.
Here is an example to test behavior of daemon threads in case of jvm exit due to non existence of user threads.
Please note second last line in the output below, when main thread exited, daemon thread also died and did not print finally executed9 statement within finally block. This means that any i/o resources closed within finally block of a daemon thread will not be closed if JVM exits due to non existence of user threads.
public class DeamonTreadExample {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
int count = 0;
while (true) {
count++;
try {
System.out.println("inside try"+ count);
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
System.out.println("finally executed"+ count);
}
}
});
t.setDaemon(true);
t.start();
Thread.currentThread().sleep(10000);
System.out.println("main thread exited");
}
}
Output
inside try1
finally executed1
inside try2
finally executed2
inside try3
finally executed3
inside try4
finally executed4
inside try5
finally executed5
inside try6
finally executed6
inside try7
finally executed7
inside try8
finally executed8
inside try9
finally executed9
inside try10
main thread exited
Daemon thread is just like a normal thread except that the JVM will only shut down when the other non daemon threads are not existing. Daemon threads are typically used to perform services for your application.
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.
Points to Note :
Any thread created by main thread, which runs main method in Java is by default non daemon because Thread inherits its daemon nature from the Thread which creates it i.e. parent Thread and since main thread is a non daemon thread, any other thread created from it will remain non-daemon until explicitly made daemon by calling setDaemon(true).
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.
Difference between Daemon and Non Daemon thread in Java :
1) JVM doesn't wait for any daemon thread to finish before existing.
2) Daemon Thread are treated differently than User Thread when JVM terminates, finally blocks are not called, Stacks are not unwounded and JVM just exits.
Daemon threads are as everybody explained, will not constrain JVM to exit, so basically its a happy thread for Application from exit point of view.
Want to add that daemon threads can be used when say I'm providing an API like pushing data to a 3rd party server / or JMS, I might need to aggregate data at the client JVM level and then send to JMS in a separate thread. I can make this thread as daemon thread, if this is not a mandatory data to be pushed to server.
This kind of data is like log push / aggregation.
Regards,
Manish
Daemon thread is like daemon process which is responsible for managing resources,a daemon thread is created by the Java VM to serve the user threads.
example updating system for unix,unix is daemon process.
child of daemon thread is always daemon thread,so by default daemon is false.you can check thread as daemon or user by using "isDaemon()" method.
so daemon thread or daemon process are basically responsible for managing resources.
for example when you starting jvm there is garbage collector running that is daemon thread whose priority is 1 that is lowest,which is managing memory.
jvm is alive as long as user thread is alive,u can not kill daemon thread.jvm is responsible to kill daemon threads.
For me, daemon thread it's like house keeper for user threads.
If all user threads finished , the daemon thread has no job and
killed by JVM.
I explained it in the YouTube video.
Let's talk only in code with working examples. I like russ's answer above but to remove any doubt I had, I enhanced it a little bit. I ran it twice, once with the worker thread set to deamon true (deamon thread) and another time set it to false (user thread). It confirms that the deamon thread ends when the main thread terminates.
public class DeamonThreadTest {
public static void main(String[] args) {
new WorkerThread(false).start(); //set it to true and false and run twice.
try {
Thread.sleep(7500);
} catch (InterruptedException e) {
// handle here exception
}
System.out.println("Main Thread ending");
}
}
class WorkerThread extends Thread {
boolean isDeamon;
public WorkerThread(boolean isDeamon) {
// When false, (i.e. when it's a user thread),
// the Worker thread continues to run.
// When true, (i.e. when it's a daemon thread),
// the Worker thread terminates when the main
// thread terminates.
this.isDeamon = isDeamon;
setDaemon(isDeamon);
}
public void run() {
System.out.println("I am a " + (isDeamon ? "Deamon Thread" : "User Thread (none-deamon)"));
int counter = 0;
while (counter < 10) {
counter++;
System.out.println("\tworking from Worker thread " + counter++);
try {
sleep(5000);
} catch (InterruptedException e) {
// handle exception here
}
}
System.out.println("\tWorker thread ends. ");
}
}
result when setDeamon(true)
=====================================
I am a Deamon Thread
working from Worker thread 0
working from Worker thread 1
Main Thread ending
Process finished with exit code 0
result when setDeamon(false)
=====================================
I am a User Thread (none-deamon)
working from Worker thread 0
working from Worker thread 1
Main Thread ending
working from Worker thread 2
working from Worker thread 3
working from Worker thread 4
working from Worker thread 5
working from Worker thread 6
working from Worker thread 7
working from Worker thread 8
working from Worker thread 9
Worker thread ends.
Process finished with exit code 0
Daemon threads are generally known as "Service Provider" thread. These threads should not be used to execute program code but system code. These threads run parallel to your code but JVM can kill them anytime. When JVM finds no user threads, it stops it and all daemon threads terminate instantly. We can set non-daemon thread to daemon using :
setDaemon(true)
Daemon threads are threads that run in the background as long as other non-daemon threads of the process are still running. Thus, when all of the non-daemon threads complete, the daemon threads are terminated. An example for the non-daemon thread is the thread running the Main.
A thread is made daemon by calling the setDaemon() method before the thread is started
For More Reference : Daemon thread in Java
There already are numerous answers; however, maybe I could shed a bit clearer light on this, as when I was reading about Daemon Threads, initially, I had a feeling, that I understood it well; however, after playing with it and debugged a bit, I observed a strange (to me) behaviour.
I was taught, that:
If I want the thread to die right after the main thread orderly finishes its execution, I should set it as Diamond.
What I tried:
I created two threads from the Main Thread, and I only set one of those as a diamond;
After orderly completing execution of the Main Thread, none of those newly created threads exited, but I expected, that Daemon thread should have been exited;
I surfed over many blogs and articles, and the best and clearest definition I found so far, comes from the Java Concurrency In Practice book, which very clearly states, that:
7.4.2 Daemon threads
Sometimes you want to create a thread that performs some helper
function but you don’t want the existence of this thread to prevent
the JVM from shutting down. This is what daemon threads are for.
Threads are divided into two types: normal threads and daemon threads.
When the JVM starts up, all the threads it creates (such as garbage
collector and other housekeeping threads) are daemon threads, except
the main thread. When a new thread is created, it inherits the daemon
status of the thread that created it, so by default any threads
created by the main thread are also normal threads. Normal threads and
daemon threads differ only in what happens when they exit. When a
thread exits, the JVM performs an inventory of running threads, and if
the only threads that are left are daemon threads, it initiates an
orderly shutdown. When the JVM halts, any remaining daemon threads are
abandoned— finally blocks are not executed, stacks are not unwound—the
JVM just exits. Daemon threads should be used sparingly—few processing
activities can be safely abandoned at any time with no cleanup. In
particular, it is dangerous to use daemon threads for tasks that might
perform any sort of I/O. Daemon threads are best saved for
“housekeeping” tasks, such as a background thread that periodically
removes expired entries from an in-memory cache.
JVM will accomplish the work when a last non-daemon thread execution is completed. By default, JVM will create a thread as nondaemon but we can make Thread as a daemon with help of method setDaemon(true). A good example of Daemon thread is GC thread which will complete his work as soon as all nondaemon threads are completed.
Daemon threads are those threads which provide general services for user threads (Example : clean up services - garbage collector)
Daemon threads are running all the time until kill by the JVM
Daemon Threads are treated differently than User Thread when JVM terminates , finally blocks are not called JVM just exits
JVM doesn't terminates unless all the user threads terminate. JVM terminates if all user threads are dies
JVM doesn't wait for any daemon thread to finish before existing and finally blocks are not called
If all user threads dies JVM kills all the daemon threads before stops
When all user threads have terminated, daemon threads can also be terminated and the main program terminates
setDaemon() method must be called before the thread's start() method is invoked
Once a thread has started executing its daemon status cannot be changed
To determine if a thread is a daemon thread, use the accessor method isDaemon()
Java daemon thread
[Daemon process]
Java uses user thread and daemon tread concepts.
JVM flow
1. If there are no `user treads` JVM starts terminating the program
2. JVM terminates all `daemon threads` automatically without waiting when they are done
3. JVM is shutdown
As you see daemon tread is a service thread for user treads.
daemon tread is low priority thread.
Thread inherits it's properties from parent thread. To set it externally you can use setDaemon() method before starting it or check it via isDaemon()
Daemon Thread
Threads that run in the background are called daemon threads.
Example of Daemon Threads:
Garbage Collector.
Signal Dispatcher.
Objective of Daemon Thread:
The main objective of the daemon threads is to provide support to the non-daemon threads.
Additional information about Daemon Thread:
Generally, Daemon threads run in the MIN_PRIORITY however, it is possible to run daemon threads with MAX_PRIORITY as well.
Example: Usually the GC runs with a MIN_PRIORITY priority, however, once there is a requirement for additional memory. JVM increases the priority of the GC from MIN_PRIORITY to MAX_PRIORITY.
Once the Thread has been started, it can't be changed from Daemon Thread to Non-Daemon Thread that will result in IllegalThreadStateException.
Example:
public static void main(String[] args) {
Thread.currentThread().setDaemon(true);
}
Output:
Exception in thread "main" java.lang.IllegalThreadStateException
at java.base/java.lang.Thread.setDaemon(Thread.java:1403)
If we are branching off a thread, the child thread inherits the nature of the parent thread. If the parent thread is a non-daemon thread automatically the child thread will be non-daemon as well and if the parent thread is a daemon, the child thread will be a daemon as well.
class Scratch {
public static void main(String[] args) {
CustomThread customThread = new CustomThread();
customThread.start();
}
}
class CustomThread extends Thread{
#Override
public void run() {
System.out.println(currentThread().isDaemon());
}
}
Output:
false
When the last non-daemon thread terminates, all the daemon threads get terminated automatically.
class Scratch {
public static void main(String[] args) {
System.out.println("Main Thread Started.");
CustomThread customThread = new CustomThread();
customThread.setDaemon(true);
customThread.start();
System.out.println("Main Thread Finished.");
}
}
class CustomThread extends Thread{
#Override
public void run() {
System.out.println("Custom Thread Started.");
try {
sleep(2000);
} catch (InterruptedException ignore) {}
System.out.println("Custom Thread Finished."); //Won't get executed.
}
}
Output:
Main Thread Started.
Main Thread Finished.
Custom Thread Started.
User threads versus Daemon threads in java threads
Daemon Threads
this threads in Java are low-priority threads that runs in the background to perform tasks such as garbage collection. Daemon thread in Java is also a service provider thread that provides services to the user thread.
User Threads
this threads are high-priority threads. The JVM will wait for any user thread to complete its task before terminating it
"keep in mind both User and Daemon threads wrapped upon OS threads"
Recently OpenJdk proposed Virtual threads with in project Loom (which they are User based as well) you may find more on Fibers and Continuations for the Java Virtual Machine threads in here.