From the course: Parallel and Concurrent Programming with Java 1

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Daemon thread: Java demo

Daemon thread: Java demo - Java Tutorial

From the course: Parallel and Concurrent Programming with Java 1

Start my 1-month free trial

Daemon thread: Java demo

- [Instructor] In this example program called daemon thread demo, I define a class called KitchenCleaner on line five which represents a periodic background task like garbage collection. The KitchenCleaner uses an infinite while loop on line seven to print a message that Olivia cleaned the kitchen, and will continuously repeat that message once a second. Down in the program's main method, I create and start a new KitchenCleaner thread on lines 19 and 20, then the main thread prints a series of messages that Barren is cooking, which are split up by some sleep statements and then finally a message that Barren is done on line 28. If I run this program, I see those messages displayed but after the main thread reaches the end and prints its final Barren is done message, the program doesn't exit because the KitchenCleaner thread is still going strong and will continue to run forever. So, I'll need to manually stop execution by clicking the little red square. To prevent the program from…

Contents