Java 8 Concurrency Updates

Preview:

DESCRIPTION

More at https://github.com/dmnlukasik/java-8-concurrency-updates

Citation preview

Java 8 Concurrency Updates

Damian Łukasik

Parallel Streams

ConcurrentHashMap

Striped64

CompletableFuture

@Contended

Parallel streams

List<Integer> ints = range(1, 5)

ints.parallelStream().forEach(e -> print(e));

>> 3 2 4 1 5

Parallel Streams

final int[] counter = {0};List<Integer> ints = range(1, 10000);

ints.parallelStream().map(e -> {

counter[0]++; return e;

});

>> 7329 Parallel Streams

ForkJoinPool.commonPool()

Parallel Streams

ForkJoinPool forkJoinPool = new ForkJoinPool(1);

forkJoinPool.submit(() -> ints.parallelStream()

.forEach(e -> counter[0]++)).get();

>> 10000

Parallel Streams

List<Integer> ints = range(1, 5)

List<Integer> collected = ints

.parallelStream()

.collect(Collectors.toList());

print(collected)>> ???

Parallel Streams

List<Integer> ints = range(1, 5)

List<Integer> collected = ints

.parallelStream()

.collect(Collectors.toList());

print(collected)>> 1 2 3 4 5

Parallel Streams

Collector.Characteristics.CONCURRENT

Parallel Streams

Collectors

groupingByConcurrent()toConcurrentMap()

Parallel Streams

size parallel [ns] sequential [ns] ratio1 77 51 -34%10 7695 578 -92%100 5461 6172 +13%

1000 25818 60636 +135%10000 181363 621829 +243%

100000 1287799 6829144 +430%1000000 12858699 70854694 +451%

Math.sin()

Parallel Streams

Rule 1:

Fast functions and predicates

Parallel Streams

Rule 2:

Avoid side effects

Parallel Streams

Rule 3:

Don't parallel() everything

Parallel Streams

ConcurrentHashMap

No more memory pitfall :)

ConcurrentHashMap

Concurrency level

ConcurrentHashMap

Load factor

ConcurrentHashMap

Treeification

ConcurrentHashMap

Striped64

Power behindLong/DoubleAdder

andLong/DoubleAccumulator

Striped64

LongAdder

increment()decrement()add(+/-42)sum()

Striped64

T110

Striped64

T1 T27 3

Striped64

T1 T2 T3 T43 2 4 1

Striped64

CompletableFuture

@sun.misc.Contented

class FalseSharing { int a; int b;

}

--)(----)(--AB)(----)(--

@Contended

class FalseSharing { int a; @Contended int b;

}

--)(-A##)(####)(##B#)(##

@Contended

StampedLock

Fence Intrinsics

Parallel Array Sorting

CountedCompleter

Feedback!

http://goo.gl/forms/PN3u8NY37f

Questions?