"Here's a new and interesting topic."
"It turns out you can start threads in different modes."
"A standard program (with only one thread) stops running when the main thread finishes its work. The main thread finishes executing, the program terminates, and the JVM frees its memory."
"If we launch a child thread, the program keeps running, even if the main thread finishes. The JVM does not terminate as long as there is at least one running thread. Once all the running threads are finished, the program closes."
"Well, large programs often have so-called 'service threads' whose job is to service other parts of the program. On their own, they aren't necessary. For example: removing unused objects (garbage collection), memory dumps and error logging, various reports on the current program status, and so on and so forth."
"These service threads are needed when the program is running, but they are not needed on their own."
"Yes, I understand that."
"Java lets you run a thread as a daemon. Such threads function the same as others, but if all the non-daemon threads in a program have terminated and only daemon threads are left, the JVM will close the program."
"So declaring a 'service' thread just means that it isn't considered when the program is terminated. Is that all?"
"Uhhhh... Well, you sure said that short and sweet. Essentially, that's what I wanted to tell you."
"Brevity is a talent. And talented robots are talented in everything."
"How do you start a thread as a daemon? Inherit from some DaemonThread class?"
"No, it's much simpler than that. The Thread class has a setDaemon(boolean) method. You just have to pass in true and that's it. You just need to call it before calling the start() method, before the real thread is created. You cannot change a thread's type after it starts running."
Thread thread = new LoggerThread(); thread.setDaemon(true); thread.start();
"And that's it?"
"I want to draw your attention once again to the process of creating and starting a thread."
"Creating a Thread object does not create a thread. A Thread object is not a thread. The JVM creates a thread when the start() method is called. A Thread is a special JVM object that lets you access information about a thread and gives you a little control over it."
"I see. Thanks, Ellie."
GO TO FULL VERSION