Govt Exams
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.
The interrupt() method is the proper way to stop a thread in Java. The stop() method is deprecated and dangerous.
The Runnable interface is the correct interface to implement for creating threads in Java. It has a single abstract method run().