Concurrency Flashcards

1
Q

Which interface provides two locks, one for read operations and another for write operations?

A

ReadWriteLock

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does ReadWriteLock work?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Is ReadWriteLock an interface or a class?

A

interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What package is ReadWriteLock in?

A

java.util.concurrent.locks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What methods does ReadWriteLock define?

A

readLock writeLock

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which ExecutorService method supports only Runnable objects?

A

“execute”, which is inherited from the Executor interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

is ExecutorService an interface or a class?

A

interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the hierarchy of the ExecutorService

A

ExecutorService is an interface:

Executor

Classes: AbstractExecutorService, ForkJoinPool, ScheduledThreadPoolExecutor, ThreadPoolExecutor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What classes implement the Executor?

A

AbstractExecutorService, ForkJoinPool, ScheduledThreadPoolExecutor, ThreadPoolExecutor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What classes implement the ScheduledExecutorService?

A

ScheduledThreadPoolExecutor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What classes implement the ExecutorService?

A

AbstractExecutorService, ForkJoinPool, ScheduledThreadPoolExecutor, ThreadPoolExecutor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What classes extend AbstractExecutorService?

A

ForkJoinPool, ThreadPoolExecutor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Explain AbstractExecutorService.

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a Future?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a Runnable?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the six VM thread states?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

The Executor execute methods accepts what kind of argument?

A

Runnable

object.execute(Runnable pRunnable);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is a Callable?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Explain the difference between execute and submit?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does the happens-before relationship describe?

A

A resource that makes write operations visible to all threads when they occur.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

When are modification operations not thread-safe with the ArrayList class?

A

When using an iterator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

In the ConcurrentMap class, what is the difference between the put method and the putIfAbsent and replace methods?

A

The put method does not check to see if the key already exists, whereas putIfAbsent and replace methods perform this check.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the first step in the fork/join strategy?

A

Determine if the work is small enough.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the next step in the fork/join strategy if the work is not small enough?

A

If the work is not small enough, split the work into two or more pieces.

25
Q

Which class provides cryptographically secure random numbers in a multi-threaded application?

A

SecureRandom

26
Q

When using shared variables across multiple threads, what does atomicity mean?

A

Read and write operations on the variable value are self-contained, so that only the start and end states are visible.

27
Q

Which Executors method creates a thread pool that generates new threads as needed and reuses existing threads when available?

A

newCachedThreadPool

28
Q

newCachedThreadPool

A

is an Executors method that creates a thread pool that generates new threads as needed and reuses existing threads when available.

29
Q

Which type of threading objects, Callable or Runnable, can return a value?

A

Callable

30
Q

What is the thread-safe version of the ArrayList class found in the java.util.concurrent package?

A

CopyOnWriteArrayList

31
Q

What is CopyOnWriteArrayList?

A

CopyOnWriteArrayList is a thread-safe version of the ArrayList class found in the java.util.concurrent package.

32
Q

Which collection class(es) represents a bounded, thread-safe queue of elements where elements closest to the head have been on the queue the longest time?

A

ArrayBlockingQueue, LinkedBlockingQueue

33
Q

Which collection class represents an unbounded, thread-safe queue of elements that are available only after a waiting period has expired?

A

DelayQueue

34
Q

How does the happens-before relationship prevent memory consistency errors?

A

Write operations are visible to all threads so that subsequent read/write operations are consistent.

35
Q

Which RecursiveTask method returns a result using the fork/join strategy?

A

compute

36
Q

Which recursive fork/join implementation, RecursiveAction or RecursiveTask , cannot return a value?

A

RecursiveAction

37
Q

What is the next step in the fork/join strategy if the work is small enough?

A

If the work is small enough, perform the work.

38
Q

Which Callable method returns a result or throws an exception?

A

call

39
Q

Which method is invoked on Lock objects to release the lock?

A

unlock

40
Q

Which Runnable method performs a task?

A

run

41
Q

Which algorithm is implemented by the fork/join framework?

A

work-stealing

42
Q

Which RecursiveTask method returns the computational result after all worker threads have completed their work?

A

join

43
Q

What is the thread-safe version of the HashMap class found in the java.util.concurrent package?

A

ConcurrentHashMap

44
Q

What is the thread-safe version of the TreeMap class found in the java.util.concurrent package?

A

ConcurrentSkipListMap

45
Q

Which Executors method creates a thread pool that changes in size based on the number of active tasks?

A

newFixedThreadPool

46
Q

How does the work-stealing algorithm describe task execution in multi-threaded applications?

A

Tasks are distributed across worker threads in a thread pool, but a worker thread can steal a task from other worker threads if that thread is not busy and other worker threads are unavailable

47
Q

Which keyword provides both visibility and atomicity when using shared variables across multiple threads?

A

synchronized

48
Q

Which Executors method creates a thread pool that supports a limitless queue of tasks for only one simultaneous thread at time?

A

newSingleThreadPool

49
Q

Which ExecutorService method supports both Runnable and Callable objects?

A

submit

50
Q

Which interface in the java.util.concurrent package can poll or cancel a task running on a worker thread without blocking?

A

Future

51
Q

What is the equivalent thread-safe version of “Queue” found in the java.util.concurrent package?

A

Queue is an interface and the equivalent concurrent interface is BlockingQueue.

Non threadsafe “queue” classes are LinkedList, ArrayDeque, DelayQueue, PriorityQueue

The “queue” classes that implement BlockingQueue are ArrayBlockingQueue and LinkedBlockingQueue, LinkedBlockingDeque, ConcurrentLinkedDeque,

52
Q

Which class provides the best performance when generating random numbers in a multi-threaded application?

A

ThreadLocalRandom

53
Q

When using shared variables across multiple threads, what does visibility mean?

A

The shared variable value is accessible immediately to multiple threads.

54
Q

Which keyword provides only visibility when using shared variables across multiple threads?

A

volatile

55
Q

What is the major difference between implicit locking and explicit Lock objects?

A

Lock objects can stop from acquiring a lock if it is unavailable or a timeout elapses, where implicit locks in synchronized blocks can deadlock.

56
Q

Which method is invoked on Lock objects to acquire the lock?

A

lock

57
Q

Which type of threading objects, Callable or Runnable, can throw an exception?

A

Callable

58
Q

Which class provides methods for multiple threads to increment or decrement an int counter concurrently without corruption?

A

AtomicInteger