4. Java 7: Concurrency Flashcards

1
Q

Name the three new interfaces and their purpose added to the Collections framework since Java7

A
  1. BlockingQueue defines a first-in-first-out data structure that blocks or times out when you attempt to add to a full queue or retrieve from an empty queue.
  2. ConcurrentMap subinterface of Map. That defines atomic operations. Remove or replace key-value only if the key is present or add a key-value pair only if absent.
  3. ConcurrentNavigableMap subinterface of ConcurrentMap that supports approximate matches.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are two new implementations in the collections framework

A
  1. CopyOnWriteArrayList: thread-safe variant of the ArrayList in wich all mutative operations are implemented by making a fresh copy of the underlying array.
  2. CopyOnWriteSet: variation on the CopyOnWriteArrayList as a Set implementation.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When should the CopyOnWriteArrayList and/or CopyOnWriteSet be used?

A

When the number of read operations vastly outnumber the number of write operations.

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

What are the new Atomic classes available in Java?

A
  • AtomicBoolean cannot be used as a replacement for Boolean.
  • AtomicInteger cannot be used as a replacement for Integer, however it does extend from Number so it can be used by utilities and tools.
  • AtomicIntegerArray
  • AtomicLong cannot be used as a replacement for Long, however it does extend from Number so it can be used by utilities and tools
  • AtomicLongArray
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Name the methods for getting the value of an AtomicInteger and after the get perform an mutation.

A
  • int getAndAdd(int delta);
  • int getAndDecrement();
  • int getAndIncrement();
  • int getAndSet(int newValue);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Name the method for first mutating an AtomicInteger and then get the new value.

A
  • int incrementAndGet();
  • int decrementAndGet();
  • int addAndGet(int delta);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the four new interfaces or abstract classes provided for Locks in Java?

A
  • Lock interface
  • ReadWriteLock interface
  • Condition interface
  • AbstractQueueSynchronizer abstract-class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
Rewrite the following class using a ReentrantLock:
public class Counter {
  private int count = 0;
  public int inc() {
     synchronized(this) {
        return ++count;
     }
  }

}

A
public class Counter {
  private int count = 0;
  private Lock lock = new ReentrantLock();
  public int inc() {
    lock.lock();
    try {
      return ++count;
    } finally {
      lock.unlock();
    }
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why was there a need for Executers and ThreadPools?

A

Seperate thread-management from the application.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
How can you rewrite the following code to use a Executor (given e is a Executor implementation and r is a Runnable)?
new Thread(r).start();
A

e.execute(r);

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

What is the difference between a Thread and a Executer?

A

A thread is more specific, when started (start()) it will always start the Runnable on a new Thread. A Executer may do the same but is not bound by it, for example it could just place the Runnable on some sort of queue or delegatae the work to a existing thread.

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

What are two more high-level interfaces that can be implemented to make use of the new Executers?

A

ExecutorService (extends Executor)
Add features to Executer that help manage the lifecycle, both of the individual tasks and the executor itself.

ScheduledExecutorService
A subinterface of ExecutorService supports future and/or periodic execution of tasks.

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

What is the Callable interface used?

A

ExecutorServices can execute a List of Callable tasks. A Callable task can return a value. The interface has one method

V call() throws Exception;

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

What is the Fork/Join framework?

A

As with any ExecutorService the fork/join framework distributes tasks to worker threads in a thread pool. The framework is distinct because it uses work-stealing algorithm, worker threads can “steal” work from other threads when itself runs out of tasks to run or it has to wait for another task.

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

The Fork/Join provides two important classes, name these and their use.

A

ForkJoinPool - the core of the framework contains all the logic and algorithm for distributing the work to worker threads.
ForkJoinTask (or more specialized RecursiveTask (can return a result) and RecursiveAction) - a task that can be executed by the ForkJoinPool

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

What are the two specific methods available in a ForkJoinTask?

A

fork() - allows a ForkJoinTask to plan another ForkJoinTask for async execution.
join() - allows a ForkJoinTask to wait for completion of other ForkJoinTasks.