11. Concurrency Flashcards

1
Q

What does the java.util.concurrent.atomic package enable?

A

The java.util.concurrent.atomic package enables multithreaded applications to safely access
individual variables without locking,

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

What is an atomic operation?

A

An atomic operation is one that, for all intents and purposes appears to happen all at once.

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

Which classes does the atomic package provide?

A

The atomic package provides several classes for different data types, such as AtomicInteger, AtomicLong, AtomicBoolean, and AtomicReference to name a few.

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

Where does CAS stand for?

A

CAS stands for Compare And Swap

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

Wat does CAS do?

A
  • The value is stored in count is copied to a temporary variable
  • The temporary variable is incremented
  • Compare the value currently in count with the original value. If it is unchanged, then swap
    the old value for the new value.
    Step 3 happens atomically, if step 3 find that some other thread has already modified the value of count, then repeat step 1-3 until we increment the field without interference.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which method does provides the CAS behaviour?

A
The central method in a class like AtomicInteger is the boolean compareAndSet(int expect, int
update) method which provides the CAS behavior
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the lock package provide?

A
  • The ability to duplicate traditional synchronized blocks
  • Nonblock scoped locking – obtain a lock in one method and release it in another
  • Multiple wait/notify/notifyAll pools per lock – threads can select which pool they wait on
  • The ability to attempt to acquire a lock and take an alternative action if locking fails
  • An implementation of a multiple-reader, single-writer lock
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why choose a lock above a synchronization?

A

The reason for you to choose to use a Lock instance instead of a traditional synchronization is that one of the very powerful features is the ability to attempt to acquire a lock. You can process a different resource (lock) and come back to the failed lock later instead of just waiting for a lock to be released and thereby making more efficient use of system resources.

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

What does the tryLock() method do?

A

The tryLock() method attempts to lock the Lock instance immediately. It returns true if the locking succeeds, false if Lock is already locked. This method never blocks.

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

When does a IllegalMonitorStateException get thrown?

A

If a thread attempts to release a lock that it does not own, an
IllegalMonitorStateException will be thrown.

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

What is a reentrantReadWriteLock?

A

A reentrantReadWriteLock is not actually a Lock; it implements the ReadWriteLock interface. What a ReentrantReadWriteLock does is produce two specialized Lock instances, one to a read lock and the other to a write lock. What makes these locks unique is that multiple threads can hold the read lock at the same time, but only one thread can hold the write lock at a time.

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

Which concurrent collections contains the concurrent package?

A
  • ConcurrentHashMap
  • ConcurrentLinkedDeque
  • ConcurrentLinkedQueue
  • ConcurrentSkipListMap
  • ConcurrentSkipListSet
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a blocking queue?

A

A BlockingQueue is a type of shared collection that is used to exchange data between 2 or more threads while causing one or more of the threads to wait until the point in time when the data can be exchanged.

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

What is the producer-consumer scenario?

A

In a producer-consumer scenario, one thread produces data, then adds it to the queue, and another thread must consume the data from the queue.

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

What are the BlockingQueue implementations?

A
  • ArrayBlockingQueue
  • LinkedBlockingDeQue
  • LinkedBlockingQueue
  • PriorityBlockingQueue
  • DelayQueue
  • LinkedTransferQueue
  • SynchronousQueue
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does a blocking queue behave?

A

A blocking collection, depending on the method being called, may cause a thread to block until another thread calls a corresponding method on the collection. Don’t call a blocking operation in a thread unless it is safe for that thread to block.

17
Q

What does CyclicBarrier do?

A

Whereas blocking queues force threads to wait based on capacity or until a method is called on the queue, a CyclicBarrier can force threads to wait at a specific point in the execution until all threads reach that point before continuing.

18
Q

What do executers do?

A

Executors (and the ThreadPools used by them) help meet two of the same needs as Threads do:

  • Creating and scheduling some java code for executing
  • Optimizing the execution of that code for the hardware resources you have available.
19
Q

What is an executor?

A

An Executor (high-level multithreading) is an alternative to starting new threads.

20
Q

Which three types of ExecutorService can be created by factory methods?

A
  • Cached Thread pool executors
  • Fixed Thread pool executors
  • Single Thread pool executors
21
Q

What does a Cached Thread pool do?

A

it will create new threads as they are needed and reuse threads that have become free.
o Threads that have been idle for 60 seconds are removed from the pool
o Without some type of external limitation, a cached thread pool may be used to
create more threads than your system can handle
 ExecutorService ex = Executors.newCachedThreadPool()

22
Q

What does a Fixed Thread pool do?

A

it is constructed using a numeric argument that specifies the number of threads used to execute tasks.
o It prevents an application from overloading a system with too many threads.

o Tasks that cannot be executed immediately are placed on an unbounded queue for
later execution
 ExecutorService ex = Executors.newFixedThreadPool(4);

23
Q

What does a Single Thread pool do?

A

it uses a single thread to execute tasks
o Tasks that cannot be executed immediately are placed on an unbounded queue for
later execution.
o Unlike a fixed thread pool executor with size of 1, a single thread executor prevents
any adjustments to the number of threads in the pool
 ExecutorService ex = Executors.newSingleThreadExecutor();

24
Q

What does a Scheduled Thread pool do?

A

This enables tasks to be executed after a delay or at repeating intervals.

25
Q

What is the benefit of using a Callable?

A

The primary benefit of using a Callable is the ability to return a result.

26
Q

What is ForkJoinPool?

A

You will typically submit a single task to a ForkJoinPool and await its completion.

27
Q

What is ForkJoinTask?

A

ForkJoinTask instance is created to represent the task that should be accomplished. This is different
from other executor services that primarily used either Runnable or Callable. A ForkJoinTask concrete
subclass has many methods, but the following are important: compute(), fork() and join().

28
Q

What is a RecursiveAction?

A
If you need to create a ForkJoinTask that does not return a result, then you should subclass
ResurciveAction. ResurciveAction extends ForkJoinTasl and has a single abtract compute method.
29
Q

What is a RecursiveTask?

A
If you need to create a ForkJoinTask that does return a result, then you should subclass
RecursiveTask.