threads
A thread is a lightweight, independent, and concurrent unit of execution within a program
Threads allow multiple tasks to be performed concurrently, and each stack has its own call stack & program counter
Ways to create a thread
extends
Thread
implements
Runnable
Both have similar implementations , but to use them ...
When to use which implementation
extends Thread
Creates the functionality directly in a thread
Use this when you want to create a new type of thread with customized behavior
implements Runnable
Creates a functionality which can be used in the constructor of a thread
Use this when you want to define a task that can be executed by multiple threads without creating new thread types.
Thread Lifecycle
Daemon Threads
Daemon threads are low-priority threads that run in the background, providing services to non-daemon (user) threads.
The key distinction is that the JVM doesn't wait for daemon threads to finish before it exits. If all non-daemon (user) threads have completed, the JVM will exit, even if daemon threads are still running.
Last updated