Skip to content
javaadvanced

Java Concurrent Programming

Threads, locks and concurrency utilities

7 questions

By EZ4Code Team

1. What are the two common ways to create a thread?

Extending the Thread class or implementing the Runnable interface
Only by extending Thread
Only by implementing Runnable
Through Serializable
Explanation: You can extend Thread and override run(), or implement Runnable and pass it to a Thread constructor; Callable + ExecutorService is also possible.

2. What does the synchronized keyword do?

Implements a mutual exclusion lock, ensuring only one thread executes the critical section at a time
Declares a constant
Makes variables visible
Asynchronous execution
Explanation: synchronized implements a mutual exclusion lock, can modify methods or code blocks, ensures mutually exclusive access, and provides memory visibility (flushes main memory when releasing the lock).

3. What does the volatile keyword guarantee?

Memory visibility of a variable, but not atomicity of compound operations
Atomicity of a variable
Mutual exclusion lock
Thread blocking
Explanation: volatile guarantees visibility (read directly from main memory, write flushed immediately) and prevents instruction reordering, but does not guarantee atomicity of compound operations like i++.

4. What is the advantage of ReentrantLock over synchronized?

Supports interruptible, timed, fair locking and multiple condition variables
Always better performance
No need to lock/unlock
Automatically releases the lock
Explanation: ReentrantLock provides advanced features like interruptible lockInterruptibly, timed tryLock, fair locks, and multiple Conditions, but requires manual unlock, typically in a finally block.

5. What is the characteristic of ConcurrentHashMap?

Thread-safe with lock-free reads and segment/node-level locking for writes, high concurrency
Locks the entire map
Disallowing null keys and values is due to design oversight
Is not thread-safe
Explanation: ConcurrentHashMap is a thread-safe concurrent Map with lock-free reads and per-bucket-node locking for writes (JDK8+), high concurrency; null keys/values are disallowed to avoid ambiguity.

6. What is the difference between CountDownLatch and CyclicBarrier?

CountDownLatch is one-time, count decrements to 0 to wake waiters; CyclicBarrier is resettable, waits for N threads to reach the barrier
They are completely identical
CountDownLatch is resettable
CyclicBarrier is not resettable
Explanation: CountDownLatch is one-time; await blocks until the count reaches zero; CyclicBarrier is reusable, N threads reach the barrier and are all released together, with an optional barrier action.

7. What is the advantage of ExecutorService over manually creating new Thread?

Reuses threads, controls concurrency, unified management and shutdown
Always worse performance
Cannot submit Runnable
Cannot submit Callable
Explanation: Thread pools reuse threads, limit concurrency, provide task queues and lifecycle management, avoiding the overhead of frequently creating and destroying threads.

More java Quizzes