Recently,I read “Java Network Programming”,in section 5.5,part 5.5.2.6 ,it has the following code:
public TimeSlicer(long milliseconds, int priority) {
this.timeslice = milliseconds;
this.setPriority(priority);
// If this is the last thread left, it should not
// stop the VM from exiting
this.setDaemon(true);
}
just not quite understand the comments,what is the relationship of a daemon thread with the VM exiting?Thank you in advance.
The Java VM exits when there are no non-daemon threads left running. By marking a thread as a daemon thread using
setDaemon(true), you are telling the VM that it is okay to exit even if this thread is still left running.From the
java.lang.Threaddocumentation: