SCJP Java Threads and Concurrency 5 — Questions and Answers
Question 1: What is the role of the `Executor` framework in Java concurrency?
- It replaces the synchronized keyword with a faster alternative
- It provides a higher-level API for managing thread creation and execution (Correct answer)
- It automatically detects and resolves deadlocks
- It is used exclusively for scheduling periodic tasks
Correct answer: It provides a higher-level API for managing thread creation and execution
The `Executor` framework decouples task submission from thread management, enabling thread pooling and flexible execution policies.
Question 2: Which interface must be implemented to submit a task to an `ExecutorService` that returns a result?
- Runnable
- Thread
- Callable (Correct answer)
- Future
Correct answer: Callable
`Callable<V>` is like `Runnable` but its `call()` method returns a value and can throw checked exceptions.
Question 3: A thread is in the TIMED_WAITING state. Which call most likely caused this?
- thread.start()
- synchronized(obj) { obj.wait(5000); } (Correct answer)
- thread.interrupt()
- ExecutorService.submit(task)
Correct answer: synchronized(obj) { obj.wait(5000); }
Calling `wait(timeout)` with a positive timeout places the thread in TIMED_WAITING until the timeout expires or it is notified.
Question 4: What exception is thrown when a thread that is blocked in `sleep()` or `wait()` has its `interrupt()` method called?
- ThreadInterruptException
- InterruptedException (Correct answer)
- ConcurrentModificationException
- IllegalStateException
Correct answer: InterruptedException
`InterruptedException` is a checked exception thrown when a thread's interrupted status is set while it is blocked in a wait, sleep, or join.
Question 5: Which `java.util.concurrent` class implements a blocking queue that can be used for producer-consumer problems?
- LinkedList
- ArrayDeque
- LinkedBlockingQueue (Correct answer)
- PriorityQueue
Correct answer: LinkedBlockingQueue
`LinkedBlockingQueue` blocks the producer when full and the consumer when empty, making it ideal for producer-consumer scenarios.
Question 6: What does the `join()` method on a Thread object do?
- Merges two threads into a single thread of execution
- Causes the calling thread to wait until the target thread has finished executing (Correct answer)
- Adds the thread to a thread pool for reuse
- Forces the target thread to terminate
Correct answer: Causes the calling thread to wait until the target thread has finished executing
Calling `threadA.join()` blocks the calling thread until `threadA` completes, allowing threads to coordinate completion order.
Question 7: Which statement about the `synchronized` keyword applied to a static method is correct?
- It locks on the instance that calls the method
- It locks on the Class object associated with the declaring class (Correct answer)
- Static methods cannot be synchronized in Java
- It creates a new lock object for each invocation
Correct answer: It locks on the Class object associated with the declaring class
A synchronized static method acquires the intrinsic lock on the `Class` object, so all instances share the same lock for that method.
What is the role of the `Executor` framework in Java concurrency?