This presentation helps discover some of the specific features of Java 8, (in particular, atomics and parallelism) and start using them effectively.
This presentation by Maksym Voronyi (Software Engineer, GlobalLogic) was delivered at Java.io 3.0 conference in Kharkiv on March 24, 2016, and GlobalLogic Mykolaiv Java Conference on June 11, 2016.
3. 3
History
Text
The Dream Time sharing
Stone Age Intel 80386
Multiple
data
multiple
handlers
Multiple
processo
rs
Multiple
data
single
handler
Hyper threading
IBM 360
CUDA
Multicore
Today
7. 7
Nicety #1 - Java 8 Atomics
http://ashkrit.blogspot.com/2014/02/atomicinteger-java-7-vs-java-8.html
fetch-and-add vs compare-and-swap (CAS)
JDK 8 - AtomicInteger
public final int getAndIncrement() {
return unsafe.getAndAddInt(
this, valueOffset, 1);
}
JDK7 - AtomicInteger
public final int getAndIncrement() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return current;
}
}
8. 8
Why Atomic important?
Text
ConcurrentHashMap<>
Collections.synchronizedMap(new HashMap<>())
• ConcurrentLinkedDeque/Queue
• ConcurrentSkipListSet/Map
• ConcurrentHashMap
• wait-free, if every concurrent operation is guaranteed to be
finished in a finite number of steps
• lock-free, if some concurrent operations are guaranteed to
be finished in a finite number of steps.
DARE
develop concurrent hash-map
TODAY tomorrow
12. 12
.parallelStream()
Text
//from Oracle tutorial:
double average = roster
.parallelStream()
.filter(p -> p.getGender() == Person.Sex.MALE)
.mapToInt(Person::getAge)
.average()
.getAsDouble()
• Java runtime partitions the stream into
multiple substreams. Aggregate operations
iterate over and process these substreams in
parallel and then combine the results
static ExecutorService newWorkStealingPool()
static ExecutorService newFixedThreadPool(int nThreads)
static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
static ExecutorService newCachedThreadPool(ThreadFactory threadFactory)
• Work Stealing (An idle thread steals work from a thread having tasks queued up more than it
can process currently)
• Ability to recursively decompose the tasks and collect the results. (Apparently, this requirement
must have popped up along with the conception of the notion of parallel processing... but lacked
a solid implementation framework in Java till Java 7)
ForkJoinPool
13. 13
The trap!
List<SomeClass> list = // A list of objects
list.parallelStream()
.map(this::veryLongProcessing)
.collect(toList());
partition
Stream-1
Stream-2
Stream-3
Core -1 Core -2
ForkJoinPool forkJoinPool = new ForkJoinPool(2);
forkJoinPool.submit(() ->
list.parallel()
.map(this::veryLongProcessing)
.collect(toList())
).get();
"Arranges to asynchronously execute this task in the pool the
current task is running in, if applicable, or using the
ForkJoinPool.commonPool() if not in
ForkJoinPool()"
15. 15
Future direction
• How Databases works (SQL& NoSQL)
• Quantum computing
• Advanced data structures (R-Tree, B-Tree, Trie, Skip-lists, Heaps…)
http://goo.gl/forms/9LauaeJBlO
N