Govt Exams
Once a thread completes (reaches TERMINATED state), calling start() again throws IllegalThreadStateException. A thread can only be started once.
Callable<V> can return a value of type V and throw checked exceptions. Runnable has void run() method and cannot throw checked exceptions.
Deadlock occurs when multiple threads are blocked indefinitely, each waiting for a resource held by another, creating a circular wait condition.
interrupt() sets an internal interrupt flag. The thread must check this flag using isInterrupted() or interrupted() and handle it appropriately.
A Semaphore maintains a count allowing multiple threads (based on permit count) to access a resource. A Mutex (binary semaphore with count=1) allows only one thread.
notifyAll() wakes up all threads waiting on the monitor. They will then compete to acquire the lock when it's released.
wait() releases the monitor lock and causes the thread to wait until notify() or notifyAll() is called by another thread, enabling inter-thread communication.
CountDownLatch is a one-time use synchronizer. Once the count reaches zero, it cannot be reset. CyclicBarrier is reusable.
CyclicBarrier allows a set of threads to wait for each other to reach a common point, then all proceed together. It's reusable unlike CountDownLatch.
volatile int counter = 0;
counter++; // in multiple threads
Which issue may occur?
volatile only ensures visibility, not atomicity. counter++ is actually three operations (read, increment, write), so even with volatile, race conditions can occur.