Chapter TWENTY-SIX
Thread Basics


Exam Objectives

Create worker threads using Runnable, Callable and use an ExecutorService to concurrently execute tasks.
Identify potential threading problems among deadlock, starvation, livelock, and race conditions.

Answers

1. The correct answer is B.
submit() only takes either a Runnable or a Callable instance as an argument. The method signature of the Callable interface doesn't take any argument and returns a value.


2. The correct answers are C and D.
Option A is false. The submit() method accepts a Runnable and returns a Future object.
Option B is false. Executors don't implement AutoCloseable. They are closed by calling the shutdown() or shutdownNow() methods.
Option C is true. A Callable task can be canceled with the Future.cancel() method.
Option D is true. A thread pool contains worker threads, which are generic threads that can execute any Runnable or Callable task.


3. The correct answer is A.
The get(long, TimeUnit) method waits for at most the given time for the task to complete, and then retrieves its result. If there's no result yet, an exception is thrown.


4. The correct answer is B.
One thread locks A and is trying to acquire the lock of B. At the same another thread is doing the opposite, it locks B and is trying the acquire the lock of A, so they are permanently blocked. This is a deadlock situation.