In this video, learn how to create a daemon thread to execute background tasks with an example Java program. Discover how a non-daemon thread blocks the main thread from terminating, whereas a daemon thread allows the program to complete while the daemon thread is still alive.
- [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. …
- Parallel vs. sequential computing
- Shared vs. distributed memory
- Execution scheduling
- Thread lifecycle
- Mutual exclusion
- Synchronized methods
- Checking for liveness
Skill Level Advanced
Java Essential Training: Objects and APIswith David Gassner2h 46m Intermediate
Java Essential Training: Syntax and Structurewith David Gassner3h 9m Intermediate
1. Parallel Computing Hardware
2. Threads and Processes
3. Mutual Exclusion
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.