☕ Java Intermediate

What is a thread in Java?

Answer

A thread is the smallest unit of execution within a program. Java is multi-threaded, meaning multiple threads can run concurrently within the same process, sharing the same heap memory but each having its own stack. Create threads by either extending Thread class and overriding run(), or implementing the Runnable interface and passing it to a Thread: new Thread(runnable).start(). Call start() (not run()) to begin execution in a new thread — calling run() directly executes it on the current thread. Thread states: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED.