SCJP Java Threads and Concurrency 4 — Questions and Answers
Question 1: What happens when you call `Thread.sleep(0)` in Java?
- The thread terminates immediately
- The thread yields the CPU to other threads of equal or higher priority (Correct answer)
- The thread blocks indefinitely until interrupted
- Nothing happens; the call is ignored
Correct answer: The thread yields the CPU to other threads of equal or higher priority
`Thread.sleep(0)` causes the current thread to yield the processor, allowing other threads of equal or higher priority to execute.
Question 2: Which method in the `Object` class causes the current thread to release its lock and wait until another thread calls `notify()` or `notifyAll()`?
- sleep()
- yield()
- wait() (Correct answer)
- join()
Correct answer: wait()
`wait()` releases the object's monitor lock and suspends the thread until it is notified or interrupted.
Question 3: A thread is in the WAITING state. Which of the following can transition it back to the RUNNABLE state?
- Calling Thread.sleep() on it
- Another thread calling notify() on the object it is waiting on (Correct answer)
- The thread's time slice expiring
- Calling Thread.yield()
Correct answer: Another thread calling notify() on the object it is waiting on
A thread in WAITING state is moved to RUNNABLE when another thread calls `notify()` or `notifyAll()` on the object the waiting thread holds a lock on.
Question 4: What is the effect of declaring a variable as `volatile` in Java?
- It prevents multiple threads from reading the variable simultaneously
- It guarantees that reads and writes to the variable are atomic for all types
- It ensures that changes to the variable are immediately visible to all threads (Correct answer)
- It makes the variable immutable across threads
Correct answer: It ensures that changes to the variable are immediately visible to all threads
`volatile` ensures that every read of the variable reflects the most recently written value, preventing CPU cache inconsistencies between threads.
Question 5: Which of the following correctly describes a 'deadlock' in Java threading?
- A thread that calls wait() without a corresponding notify()
- Two or more threads are each waiting for a lock held by the other, causing all to block forever (Correct answer)
- A thread that runs an infinite loop without yielding
- A situation where a thread is starved of CPU time by higher-priority threads
Correct answer: Two or more threads are each waiting for a lock held by the other, causing all to block forever
Deadlock occurs when two or more threads form a circular dependency on locks, each waiting for the other to release its lock.
Question 6: What is the output of the following code if two threads call `increment()` concurrently on the same instance? ```java class Counter { int count = 0; public void increment() { count++; } } ```
- Always exactly 2
- Always exactly 1
- Exactly 2 due to Java's memory model guarantees
- Unpredictable; may be 1 or 2 due to a race condition (Correct answer)
Correct answer: Unpredictable; may be 1 or 2 due to a race condition
`count++` is not atomic; it involves read, increment, and write steps, so concurrent access without synchronization can cause lost updates.
Question 7: Which class in `java.util.concurrent.atomic` provides a thread-safe integer that supports atomic increment operations?
- SynchronizedInteger
- AtomicInteger (Correct answer)
- ConcurrentInt
- ThreadSafeInteger
Correct answer: AtomicInteger
`AtomicInteger` uses low-level compare-and-swap (CAS) hardware instructions to perform atomic operations without explicit synchronization.
What happens when you call `Thread.sleep(0)` in Java?