A developer needs to ensure that exactly 5 threads complete their tasks before proceeding to the next phase. Which synchronization utility is most appropriate?
ASemaphore
BCountDownLatch
CPhaser
DMutex
Correct Answer:
B. CountDownLatch
EXPLANATION
CountDownLatch is designed for one-time barrier scenarios where threads wait for a countdown to reach zero. With an initial count of 5, it perfectly suits this use case. Semaphore is reusable, Phaser handles multiple phases, and Mutex is not a Java class.
Which of the following methods will cause a thread to release all locks it holds while waiting?
Await()
Bsleep()
Cyield()
Djoin()
Correct Answer:
A. wait()
EXPLANATION
The wait() method causes a thread to release all acquired locks and enter a waiting state until notify() or notifyAll() is called. sleep(), yield(), and join() do not release locks.
What is the difference between start() and run() methods in threading?
Astart() creates a new thread, run() executes in the same thread
Brun() creates a new thread, start() executes in the same thread
CBoth are identical
Dstart() is deprecated in Java 21
Correct Answer:
A. start() creates a new thread, run() executes in the same thread
EXPLANATION
start() creates a new thread and calls run() in that new thread, while directly calling run() executes it in the current thread without creating a new thread.
Which method must be implemented to create a thread in Java?
Astart()
Brun()
Cexecute()
Dbegin()
Correct Answer:
B. run()
EXPLANATION
The run() method must be implemented when creating a thread either by extending Thread class or implementing Runnable interface. The start() method calls run() internally.
What is the output of the following code?
Thread t = new Thread(() -> System.out.println(Thread.currentThread().getName()));
t.start();
AThread-0
Bmain
CCompilation error
DRuntime exception
Correct Answer:
A. Thread-0
EXPLANATION
When a new Thread is created without a name parameter, it gets a default name 'Thread-n' where n is a counter. The thread will print 'Thread-0' as it is the first thread created.
Which method is used to prevent race conditions by allowing only one thread to access a resource at a time?
Asynchronized
Bvolatile
Catomic
Dtransient
Correct Answer:
A. synchronized
EXPLANATION
The synchronized keyword creates a critical section that only one thread can access at a time, preventing race conditions. volatile ensures visibility but not atomicity.
Which collection class is thread-safe without explicit synchronization?
AArrayList
BHashMap
CConcurrentHashMap
DTreeMap
Correct Answer:
C. ConcurrentHashMap
EXPLANATION
ConcurrentHashMap uses segment-based locking (in older versions) or fine-grained locking to provide thread-safety without synchronizing the entire map. Other options require external synchronization or Collections.synchronizedMap().
Which of the following will cause a deadlock situation?
ATwo threads waiting for locks held by each other in a circular manner
BA thread acquiring the same lock twice
CA thread calling Thread.sleep() while holding a lock
DUsing ExecutorService with a fixed thread pool
Correct Answer:
A. Two threads waiting for locks held by each other in a circular manner
EXPLANATION
Deadlock occurs when two or more threads are waiting indefinitely for locks held by each other. Option B creates a reentrant situation (allowed in ReentrantLock). Option C is problematic but not necessarily deadlock. Option D is safe if used properly.