Govt. Exams
Entrance Exams
The volatile keyword ensures that any read of a volatile variable will read the most recent write by any thread. It provides visibility guarantees but not atomicity, making it lighter than synchronization for simple flag checks.
A thread can only exist in one state at a time (NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, or TERMINATED). Calling start() multiple times throws IllegalThreadStateException. The start() method should be called, not run() directly.
The Runnable interface contains a single run() method that defines the code to be executed when a thread runs. It's preferred over extending Thread class.
The join() method returns void. It causes the calling thread to wait until the thread on which it is called completes its execution.
InterruptedException is thrown when a thread is interrupted during sleep(), wait(), or join() operations.
notify() wakes up a single arbitrary thread that is waiting on the same monitor. notifyAll() wakes up all waiting threads.
The synchronized keyword ensures that only one thread can execute a critical section at a time, preventing race conditions and data inconsistency.
Thread.sleep() pauses thread execution for specified milliseconds while retaining all locks. pause(), halt(), and suspend() are not standard Java methods for this purpose.
Both extending Thread class and implementing Runnable interface are valid approaches to create threads in Java.
The join() method causes the current thread to wait until the thread on which it is called completes its execution.