Entrance Exams
Govt. Exams
ConcurrentHashMap is designed for concurrent access and is thread-safe. ArrayList, HashMap, and TreeMap require external synchronization for thread safety.
The join() method returns void. It causes the calling thread to wait until the thread on which it is called completes its execution.
When threads wait indefinitely for each other to release resources, it's a deadlock. This scenario is a classic deadlock example.
InterruptedException is thrown when a thread is interrupted during sleep(), wait(), or join() operations.
java
class Test extends Thread {
public void run() {
System.out.print("T");
}
}
public class Main {
public static void main(String[] args) {
Test t = new Test();
t.run();
t.start();
}
}
First t.run() executes directly (prints T), then t.start() creates a new thread and calls run() again (prints T). So output is TT.
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.
This is a classic circular wait scenario resulting in deadlock, where neither thread can proceed because each is waiting for a resource held by the other.
Thread pooling reuses a fixed set of threads instead of creating new ones for each task. ExecutorService provides convenient methods for thread pool management.