Java Threads and Concurrency Flashcards
7 cards from real SCJP practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Java Threads and Concurrency flashcards as text
What happens when you call `Thread.sleep(0)` in Java?
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.
Which method in the `Object` class causes the current thread to release its lock and wait until another thread calls `notify()` or `notifyAll()`?
Answer: wait()
`wait()` releases the object's monitor lock and suspends the thread until it is notified or interrupted.
A thread is in the WAITING state. Which of the following can transition it back to the RUNNABLE state?
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.
What is the effect of declaring a variable as `volatile` in Java?
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.
Which of the following correctly describes a 'deadlock' in Java threading?
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.
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++; } } ```
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.
Which class in `java.util.concurrent.atomic` provides a thread-safe integer that supports atomic increment operations?
Answer: AtomicInteger
`AtomicInteger` uses low-level compare-and-swap (CAS) hardware instructions to perform atomic operations without explicit synchronization.
How this page was made: written with AI tools and published by Nathan Beja, founder of Practice Test Geeks, who is responsible for its content. We have not yet matched this exam's facts to an official source, so confirm fees, dates and rules with the exam body. Not reviewed by a subject-matter expert. Editorial policy