Concurrent Collections Flashcards Preview

Java8 > Concurrent Collections > Flashcards

Flashcards in Concurrent Collections Deck (24)
Loading flashcards...
1

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

2

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

3

What is Linked Blocking queue?

Maintains a linked list between elements

4

What is LinkedBlockingDeque?

LinkedBlockingQueue class maintains the doubly linked list between elements and implements BlockingDeque interface which inturn extends Deque

5

What are the methods in LinkedBlockingDeque?

1. offerFirst(E e, long timeout, TimeUnit time)
2. offerlast(E e, long timeout, TimeUnit unit)
3. pollFirst(long timeout, TimeUnit unit)
4. pollLast(long timeout, TimeUnit unit)

6

What are the classes under SkipList?

ConcurrentSkipListSet and concurrentSkipListMap.

They are the counterparts of TreeSet and TreeMap

7

What should be kept on mind on seeing SkipList and SkipMap on the exam?

They are Sorted concurrent collections

8

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.

9

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

10

What is the problems with CopyOnWriteCollections?

It can occupy lots of space in memory since a new collection structure needs to be allocated anytime

11

Where does CopyOnWrite classes mostly being used?

In multi-threaded environment, where reads are far more common than writes

12

Best practice while Using Synchronized collections?

1. 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

13

What is the most powerful features of Stream API?

It has a built in concurrency support

14

What is serial stream?

A stream in which the results are ordered, with only one entry being processed at a time

15

What is a parallel streams?

A stream that is capable of processing results concurrently using multiple threads

16

How can we create a parallel streams?

1. using parallel() on existing streams
2. Calling parallelStream() that can be called on any collections.

The collection interface includes a method parallelStream()

17

What is the problem of using the parallelStream()

The elements will not be processed in order

18

What is the significance of Parallel streams regarding performance?

1. They improve the performance that many stream operations can be executed independently
2. Which means on operation does not impact the results of the other

19

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

20

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

21

What is strongly recommended in using parallel streams?

To avoid stateful operations when using parallel streams

22

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

23

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.

24

What are parallel reductions?

Reduction operations on parallel streams are referred as parallel reductions