Name the three new interfaces and their purpose added to the Collections framework since Java7
What are two new implementations in the collections framework
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?
Name the methods for getting the value of an AtomicInteger and after the get perform an mutation.
Name the method for first mutating an AtomicInteger and then get the new value.
What are the four new interfaces or abstract classes provided for Locks in Java?
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
What are the two specific methods available in a ForkJoinTask?
fork() - allows a ForkJoinTask to plan another ForkJoinTask for async execution.
join() - allows a ForkJoinTask to wait for completion of other ForkJoinTasks.