Concurrent Collections Flashcards

1
Q

What is Blocking Queue?

A

Blocking Queue is just like a queue except that it includes methods that will wait a specific amount of time to complete an operation

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

What are the blocking queue waiting methods?

A

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

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

What is Linked Blocking queue?

A

Maintains a linked list between elements

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

What is LinkedBlockingDeque?

A

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

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

What are the methods in LinkedBlockingDeque?

A
  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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the classes under SkipList?

A

ConcurrentSkipListSet and concurrentSkipListMap.

They are the counterparts of TreeSet and TreeMap

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

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

A

They are Sorted concurrent collections

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

What is special on CopyOnWriteArrayList and CopyOnWriteArraySet concurrent classes?

A

These classes copy all their elements to a new underlying structure anytime an element is added, modified or removed from collections.

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

What does a modified element mean?

A

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

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

What is the problems with CopyOnWriteCollections?

A

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

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

Where does CopyOnWrite classes mostly being used?

A

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

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

Best practice while Using Synchronized collections?

A
  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

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

What is the most powerful features of Stream API?

A

It has a built in concurrency support

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

What is serial stream?

A

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

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

What is a parallel streams?

A

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

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

How can we create a parallel streams?

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

The collection interface includes a method parallelStream()

17
Q

What is the problem of using the parallelStream()

A

The elements will not be processed in order

18
Q

What is the significance of Parallel streams regarding performance?

A
  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
Q

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);

A

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
Q

What is stateful lambda expression?

A

Stateful lambda expression is one whose results depends on any state that might change during the execution of the pipeline

21
Q

What is strongly recommended in using parallel streams?

A

To avoid stateful operations when using parallel streams

22
Q

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+” “);
}

A

forEachOrdered will print the data in sequence, whereas ‘data’ may contain data in random manner

23
Q

Why should we use a parallel streams to a synchronized collections?

A

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
Q

What are parallel reductions?

A

Reduction operations on parallel streams are referred as parallel reductions