4. Java 7: Concurrency Flashcards
Name the three new interfaces and their purpose added to the Collections framework since Java7
- 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.
- 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.
- ConcurrentNavigableMap subinterface of ConcurrentMap that supports approximate matches.
What are two new implementations in the collections framework
- CopyOnWriteArrayList: thread-safe variant of the ArrayList in wich all mutative operations are implemented by making a fresh copy of the underlying array.
- CopyOnWriteSet: variation on the CopyOnWriteArrayList as a Set implementation.
When should the CopyOnWriteArrayList and/or CopyOnWriteSet be used?
When the number of read operations vastly outnumber the number of write operations.
What are the new Atomic classes available in Java?
- 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
Name the methods for getting the value of an AtomicInteger and after the get perform an mutation.
- int getAndAdd(int delta);
- int getAndDecrement();
- int getAndIncrement();
- int getAndSet(int newValue);
Name the method for first mutating an AtomicInteger and then get the new value.
- int incrementAndGet();
- int decrementAndGet();
- int addAndGet(int delta);
What are the four new interfaces or abstract classes provided for Locks in Java?
- Lock interface
- ReadWriteLock interface
- Condition interface
- AbstractQueueSynchronizer abstract-class
Rewrite the following class using a ReentrantLock: public class Counter { private int count = 0;
public int inc() { synchronized(this) { return ++count; } }
}
public class Counter { private int count = 0; private Lock lock = new ReentrantLock(); public int inc() { lock.lock(); try { return ++count; } finally { lock.unlock(); } } }
Why was there a need for Executers and ThreadPools?
Seperate thread-management from the application.
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();
e.execute(r);
What is the difference between a Thread and a Executer?
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.
What are two more high-level interfaces that can be implemented to make use of the new Executers?
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.
What is the Callable interface used?
ExecutorServices can execute a List of Callable tasks. A Callable task can return a value. The interface has one method
V call() throws Exception;
What is the Fork/Join framework?
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.
The Fork/Join provides two important classes, name these and their use.
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