Threads and Runnable Tasks
A Java thread represents an execution path. Production applications generally benefit from executors that manage task scheduling and lifecycle.
Runnable task = () ->
System.out.println(Thread.currentThread().getName());
Thread thread = new Thread(task);
thread.start();
Calling start() creates a new execution path; calling run() directly is an ordinary method call on the current thread.
Shared mutable state is where concurrency becomes difficult. Ask who owns state, who can mutate it, and what synchronization establishes visibility and ordering.
start vs run
start() schedules a new thread. run() is just an ordinary method call when invoked directly.
Key Takeaways
- Prefer task-based concurrency over many manually managed threads.
- Thread safety is primarily about shared mutable state.
- Know the difference between starting a thread and invoking run directly.