Concurrent Collections Flashcards
What is Blocking Queue?
Blocking Queue is just like a queue except that it includes methods that will wait a specific amount of time to complete an operation
What are the blocking queue waiting methods?
offer(E e, long timeout, TimeUnit unit) : Adds item to the queue waiting for specified time, returning false if time elapses when disk space available
poll(long timeout, TimeUnit unit): Retrieves and removes an item from Queue, waiting specified time, returning null if time elapses
Both throw InterruptedCheckedExceptions
What is Linked Blocking queue?
Maintains a linked list between elements
What is LinkedBlockingDeque?
LinkedBlockingQueue class maintains the doubly linked list between elements and implements BlockingDeque interface which inturn extends Deque
What are the methods in LinkedBlockingDeque?
- offerFirst(E e, long timeout, TimeUnit time)
- offerlast(E e, long timeout, TimeUnit unit)
- pollFirst(long timeout, TimeUnit unit)
- pollLast(long timeout, TimeUnit unit)
What are the classes under SkipList?
ConcurrentSkipListSet and concurrentSkipListMap.
They are the counterparts of TreeSet and TreeMap
What should be kept on mind on seeing SkipList and SkipMap on the exam?
They are Sorted concurrent collections
What is special on CopyOnWriteArrayList and CopyOnWriteArraySet concurrent classes?
These classes copy all their elements to a new underlying structure anytime an element is added, modified or removed from collections.
What does a modified element mean?
They mean a refernece in the collection is changed.
Modifying the actual contents of the collection will not cause a new structure to be allocated
What is the problems with CopyOnWriteCollections?
It can occupy lots of space in memory since a new collection structure needs to be allocated anytime
Where does CopyOnWrite classes mostly being used?
In multi-threaded environment, where reads are far more common than writes
Best practice while Using Synchronized collections?
- When we use synchronized version of colections, they synchronize the access on data elements such as set() and get() on that collections, they dont synchronize access on any iterators that we create from synchronized collections.
So, we must use a synchronization block if you need to iterate over any of returned collections
What is the most powerful features of Stream API?
It has a built in concurrency support
What is serial stream?
A stream in which the results are ordered, with only one entry being processed at a time
What is a parallel streams?
A stream that is capable of processing results concurrently using multiple threads
How can we create a parallel streams?
- using parallel() on existing streams
- Calling parallelStream() that can be called on any collections.
The collection interface includes a method parallelStream()
What is the problem of using the parallelStream()
The elements will not be processed in order
What is the significance of Parallel streams regarding performance?
- They improve the performance that many stream operations can be executed independently
- Which means on operation does not impact the results of the other
What is the result of the following?
Arrays.asList(“jackal”,”kangaroo”,”lemur”)
.parallelStream()
.map(s -> {System.out.println(s); return s.toUpperCase();})
.forEach(System.out::println);
The order in which they are processed can be different.
We may can also see terminal results printing before the intermediate operations have finished
What is stateful lambda expression?
Stateful lambda expression is one whose results depends on any state that might change during the execution of the pipeline
What is strongly recommended in using parallel streams?
To avoid stateful operations when using parallel streams
What is the result of this?
List data = Collections.synchronizedList(new ArrayList<>());
Arrays.asList(1,2,3,4,5,6).parallelStream()
.map(i -> {data.add(i); return i;}) // AVOID STATEFUL LAMBDA EXPRESSIONS!
.forEachOrdered(i -> System.out.print(i+” “));
System.out.println();
for(Integer e: data) {
System.out.print(e+” “);
}
forEachOrdered will print the data in sequence, whereas ‘data’ may contain data in random manner
Why should we use a parallel streams to a synchronized collections?
Anytime
you are working with a collection with a parallel stream, it is recommended that you use
a concurrent collection. For example, if we had used a regular ArrayList rather than a
synchronized one, we could have seen output such as the following:
1 2 3 4 5 6
null 2 4 5 6 1
For an ArrayList object, the JVM internally manages a primitive array of the same type. As the
size of the dynamic ArrayList grows, a new, larger primitive array is periodically required. If
two threads both trigger the array to be resized at the same time, a result can be lost, producing
the unexpected value shown here. As briefly mentioned earlier, and also discussed later in this
chapter, the unexpected result of two tasks executing at the same time is a race condition.
What are parallel reductions?
Reduction operations on parallel streams are referred as parallel reductions