Concurrency Flashcards
Which interface provides two locks, one for read operations and another for write operations?
ReadWriteLock
How does ReadWriteLock work?
A ReadWriteLock maintains a pair of associated locks, one for read-only operations and one for writing. The read lock may be held simultaneously by multiple reader threads, so long as there are no writers. The write lock is exclusive.
Is ReadWriteLock an interface or a class?
interface
What package is ReadWriteLock in?
java.util.concurrent.locks
What methods does ReadWriteLock define?
readLock writeLock
Which ExecutorService method supports only Runnable objects?
“execute”, which is inherited from the Executor interface.
is ExecutorService an interface or a class?
interface
What is the hierarchy of the ExecutorService
ExecutorService is an interface:
Executor
Classes: AbstractExecutorService, ForkJoinPool, ScheduledThreadPoolExecutor, ThreadPoolExecutor
What classes implement the Executor?
AbstractExecutorService, ForkJoinPool, ScheduledThreadPoolExecutor, ThreadPoolExecutor
What classes implement the ScheduledExecutorService?
ScheduledThreadPoolExecutor
What classes implement the ExecutorService?
AbstractExecutorService, ForkJoinPool, ScheduledThreadPoolExecutor, ThreadPoolExecutor
What classes extend AbstractExecutorService?
ForkJoinPool, ThreadPoolExecutor
Explain AbstractExecutorService.
Provides default implementations of ExecutorService execution methods.
- Implements the submit, invokeAny and invokeAll methods.
- RunnableFuture is returned by newTaskFor method.
- The RunnableFuture is implemented by the FutureTask class.
What is a Future?
A Future is an interface. It defines methods to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation.
What is a Runnable?
Runnable is an interface that should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.
What are the six VM thread states?
NEW - A thread that has not yet started is in this state.
RUNNABLE - A thread executing in the Java virtual machine is in this state.
BLOCKED - A thread that is blocked waiting for a monitor lock is in this state.
WAITING - A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
TIMED_WAITING A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
TERMINATED A thread that has exited is in this state.
The Executor execute methods accepts what kind of argument?
Runnable
object.execute(Runnable pRunnable);
What is a Callable?
A task that returns a result and may throw an exception. Implementors define a single method with no arguments called call.
The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.
defines the call method.
V call();
V is a type argument.
Callable
Explain the difference between execute and submit?
execute
- Accepts a Runnable object
- Executes the given command at some time in the future.
- The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation.
- Returns void
submit
- Accepts a Runnable object or a Callable object
- Method submit extends base method Executor.execute(java.lang.Runnable) by creating and returning a Future that can be used to cancel execution and/or wait for completion.
- Returns a Future object
- If Runnable used then the Future’s get method will return null upon successful completion.
What does the happens-before relationship describe?
A resource that makes write operations visible to all threads when they occur.
When are modification operations not thread-safe with the ArrayList class?
When using an iterator
In the ConcurrentMap class, what is the difference between the put method and the putIfAbsent and replace methods?
The put method does not check to see if the key already exists, whereas putIfAbsent and replace methods perform this check.
What is the first step in the fork/join strategy?
Determine if the work is small enough.