the java fork-join pool framework (part 4)10 •forkjoinpool extends abstract executorservice •it...

40
The Java Fork-Join Pool Framework (Part 4) Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Upload: others

Post on 05-May-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

The Java Fork-Join Pool Framework

(Part 4)

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidt

Professor of Computer Science

Institute for Software

Integrated Systems

Vanderbilt University

Nashville, Tennessee, USA

Page 2: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

2

• Understand how the Java fork-joinframework processes tasks in parallel

• Recognize the structure & functionalityof the fork-join framework

• Know how the fork-join framework is implemented internally

• Recognize the key methods in the ForkJoinPool class

Learning Objectives in this Part of the Lesson

Page 3: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

3

• Understand how the Java fork-joinframework processes tasks in parallel

• Recognize the structure & functionalityof the fork-join framework

• Know how the fork-join framework is implemented internally

• Recognize the key methods in the ForkJoinPool class

• As well as the ForkJoinTask classhierarchy

Learning Objectives in this Part of the Lesson

Page 4: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

4

Key Methods inJava ForkJoinPool

Page 5: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

5

• ForkJoinPool extends AbstractExecutorService

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html

Key Methods in Java ForkJoinPoolclass ForkJoinPool extends

AbstractExecutorService {

...

void execute(Runnable cmd){...}

<T> Future<T> submit

(Callable<T> task){...}

<T> List<Future<T>> invokeAll

(Collection<? extends

Callable<T>> tasks){...}

<T> T invokeAny

(Collection<? extends

Callable<T>> tasks){...}

Page 6: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

6

• ForkJoinPool extends AbstractExecutorService

• It therefore implements theExecutorService methods

Key Methods in Java ForkJoinPoolclass ForkJoinPool extends

AbstractExecutorService {

...

void execute(Runnable cmd){...}

<T> Future<T> submit

(Callable<T> task){...}

<T> List<Future<T>> invokeAll

(Collection<? extends

Callable<T>> tasks){...}

<T> T invokeAny

(Collection<? extends

Callable<T>> tasks){...}

Page 7: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

7

• ForkJoinPool extends AbstractExecutorService

• It therefore implements theExecutorService methods

Key Methods in Java ForkJoinPoolclass ForkJoinPool extends

AbstractExecutorService {

...

void execute(Runnable cmd){...}

<T> Future<T> submit

(Callable<T> task){...}

<T> List<Future<T>> invokeAll

(Collection<? extends

Callable<T>> tasks){...}

<T> T invokeAny

(Collection<? extends

Callable<T>> tasks){...}

However, these methods don’t leverage the powerful fork-join pool features

Page 8: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

8

• ForkJoinPool extends AbstractExecutorService

• It therefore implements theExecutorService methods

• It also implements key methods for non-ForkJoinTask clients

Key Methods in Java ForkJoinPoolclass ForkJoinPool extends

AbstractExecutorService {

...

void execute(ForkJoinTask<T>

task)

{ ... }

T invoke(ForkJoinTask<T> task)

{ ... }

ForkJoinTask<T> submit

(ForkJoinTask<T> task)

{ ... }

Page 9: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

9

• ForkJoinPool extends AbstractExecutorService

• It therefore implements theExecutorService methods

• It also implements key methods for non-ForkJoinTask clients

Key Methods in Java ForkJoinPoolclass ForkJoinPool extends

AbstractExecutorService {

...

void execute(ForkJoinTask<T>

task)

{ ... }

T invoke(ForkJoinTask<T> task)

{ ... }

ForkJoinTask<T> submit

(ForkJoinTask<T> task)

{ ... }

These methods do leverage the powerful properties of the fork-join pool

Page 10: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

10

• ForkJoinPool extends AbstractExecutorService

• It therefore implements theExecutorService methods

• It also implements key methods for non-ForkJoinTask clients

• Arrange async exection

Key Methods in Java ForkJoinPoolclass ForkJoinPool extends

AbstractExecutorService {

...

void execute(ForkJoinTask<T>

task)

{ ... }

T invoke(ForkJoinTask<T> task)

{ ... }

ForkJoinTask<T> submit

(ForkJoinTask<T> task)

{ ... }

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html#execute

Page 11: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

11

• ForkJoinPool extends AbstractExecutorService

• It therefore implements theExecutorService methods

• It also implements key methods for non-ForkJoinTask clients

• Arrange async exection

• Perform the task, blocking until it completes

Key Methods in Java ForkJoinPoolclass ForkJoinPool extends

AbstractExecutorService {

...

void execute(ForkJoinTask<T>

task)

{ ... }

T invoke(ForkJoinTask<T> task)

{ ... }

ForkJoinTask<T> submit

(ForkJoinTask<T> task)

{ ... }

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html#invoke

Page 12: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

12

• ForkJoinPool extends AbstractExecutorService

• It therefore implements theExecutorService methods

• It also implements key methods for non-ForkJoinTask clients

• Arrange async exection

• Perform the task, blocking until it completes

• Submit a ForkJoinTask for execution, returns a future

Key Methods in Java ForkJoinPoolclass ForkJoinPool extends

AbstractExecutorService {

...

void execute(ForkJoinTask<T>

task)

{ ... }

T invoke(ForkJoinTask<T> task)

{ ... }

ForkJoinTask<T> submit

(ForkJoinTask<T> task)

{ ... }

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html#submit

Page 13: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

13

• The ForkJoinPool size defaults to the # of cores available to the JVM

Key Methods in Java ForkJoinPoolclass ForkJoinPool extends

AbstractExecutorService {

public ForkJoinPool() {

this(Math.min(MAX_CAP,

Runtime.getRuntime()

.availableProcessors()),

...);

}

public ForkJoinPool

(int parallelism) {

this(parallelism, ...);

}

...

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html#ForkJoinPool

Page 14: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

14

• The ForkJoinPool size defaults to the # of cores available to the JVM

• This size can also be controlled programmatically

class ForkJoinPool extends

AbstractExecutorService {

public ForkJoinPool() {

this(Math.min(MAX_CAP,

Runtime.getRuntime()

.availableProcessors()),

...);

}

public ForkJoinPool

(int parallelism) {

this(parallelism, ...);

}

...

Key Methods in Java ForkJoinPool

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html#ForkJoinPool

Page 15: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

15

• The common fork-join pool can beaccessed via a static method

Key Methods in Java ForkJoinPool

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html#commonPool

class ForkJoinPool extends

AbstractExecutorService {

...

static final ForkJoinPool

common;

public static ForkJoinPool

commonPool() {

return common;

}

Page 16: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

16

• The common fork-join pool can beaccessed via a static method

• The common pool is used by any ForkJoinTask that is not explicitly submitted to a specified pool

Key Methods in Java ForkJoinPoolclass ForkJoinPool extends

AbstractExecutorService {

...

static final ForkJoinPool

common;

public static ForkJoinPool

commonPool() {

return common;

}

Page 17: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

17See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html

• ForkJoinPool also provides various management & monitoring operations

int getParallelism() – Returns the targeted parallelism level of this pool

int getPoolSize() – Returns the number of worker threads that have started but not yet terminated

int getQueuedSubmissionCount() – Returns an estimate of the number of tasks submitted to this pool that have not yet begun executing

long getStealCount() – Returns an estimate of the total number of tasks stolen from one thread's work queue by another

Key Methods in Java ForkJoinPool

Page 18: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

18

Key Methods inJava ForkJoinTask

Page 19: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

19

• ForkJoinTask implements theFuture interface

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinTask.html

Key Methods in Java ForkJoinTaskabstract class ForkJoinTask<V>

implements Future<V>,

Serializable {

...

Page 20: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

20

• ForkJoinTask implements theFuture interface

• fork() enables a task to createsub-tasks that run in parallel

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinTask.html#fork

Key Methods in Java ForkJoinTaskabstract class ForkJoinTask<V>

implements Future<V>,

Serializable {

...

final ForkJoinTask<V> fork()

{ ... }

final V join() { ... }

final V invoke() { ... }

Page 21: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

21

• ForkJoinTask implements theFuture interface

• fork() enables a task to createsub-tasks that run in parallel

• Asynchronously execute this task in the current task’s poolor ForkJoinPool.commonPool()

Key Methods in Java ForkJoinTaskabstract class ForkJoinTask<V>

implements Future<V>,

Serializable {

...

final ForkJoinTask<V> fork()

{ ... }

final V join() { ... }

final V invoke() { ... }

Page 22: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

22

• ForkJoinTask implements theFuture interface

• fork() enables a task to createsub-tasks that run in parallel

• Asynchronously execute this task in the current task’s poolor ForkJoinPool.commonPool()

• Pushes the task on the head of the deque owned by the current worker thread

Key Methods in Java ForkJoinTask

Sub-Task1.1

Sub-Task1.2

Sub-Task1.3 Sub-Task3.3

Sub-Task3.4Sub-Task2.4

WorkQueue WorkQueue WorkQueue

Sub-Task1.4

2. push()1. fork()

Page 23: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

23

• ForkJoinTask implements theFuture interface

• fork() enables a task to createsub-tasks that run in parallel

• join() returns the result of a computation when it’s done

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinTask.html#join

Key Methods in Java ForkJoinTaskabstract class ForkJoinTask<V>

implements Future<V>,

Serializable {

...

final ForkJoinTask<V> fork()

{ ... }

final V join() { ... }

final V invoke() { ... }

Page 24: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

24

• ForkJoinTask implements theFuture interface

• fork() enables a task to createsub-tasks that run in parallel

• join() returns the result of a computation when it’s done

• It “blocks” the calling taskuntil forked sub-task is done

Key Methods in Java ForkJoinTaskabstract class ForkJoinTask<V>

implements Future<V>,

Serializable {

...

final ForkJoinTask<V> fork()

{ ... }

final V join() { ... }

final V invoke() { ... }

Page 25: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

25

• ForkJoinTask implements theFuture interface

• fork() enables a task to createsub-tasks that run in parallel

• join() returns the result of a computation when it’s done

• It “blocks” the calling taskuntil forked sub-task is done

• Defines a synchronization point

• Ensures all writes in a worker thread that “happen-before” join() are made visible to other threads after the join()

Key Methods in Java ForkJoinTaskabstract class ForkJoinTask<V>

implements Future<V>,

Serializable {

...

final ForkJoinTask<V> fork()

{ ... }

final V join() { ... }

final V invoke() { ... }

See stackoverflow.com/questions/4800503/memory-visibility-in-fork-join

Page 26: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

26

• ForkJoinTask implements theFuture interface

• fork() enables a task to createsub-tasks that run in parallel

• join() returns the result of a computation when it’s done

• invoke() performs this task, awaits its completion if needed, & returns its result

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinTask.html#invoke

Key Methods in Java ForkJoinTaskabstract class ForkJoinTask<V>

implements Future<V>,

Serializable {

...

final ForkJoinTask<V> fork()

{ ... }

final V join() { ... }

final V invoke() { ... }

Page 27: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

27

• ForkJoinTask implements theFuture interface

• fork() enables a task to createsub-tasks that run in parallel

• join() returns the result of a computation when it’s done

• invoke() performs this task, awaits its completion if needed, & returns its result

• Throws RuntimeException or Errorif the underlying computation did so

Key Methods in Java ForkJoinTaskabstract class ForkJoinTask<V>

implements Future<V>,

Serializable {

...

final ForkJoinTask<V> fork()

{ ... }

final V join() { ... }

final V invoke() { ... }

Page 28: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

28

Key Methods in the Java RecursiveAction

Page 29: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

29

• RecursiveAction extends theForkJoinTask superclass & does not return a result

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/RecursiveAction.html

Key Methods in Java RecursiveActionabstract class RecursiveAction

extends ForkJoinTask<Void> {

...

Page 30: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

30

• RecursiveTask extends ForkJoinTaskto return a result

• compute() must be overriddenby subclasses to perform thetask’s main computation

abstract class RecursiveAction

extends ForkJoinTask<Void> {

protected abstract Void

compute();

...

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/RecursiveTask.html#compute

Key Methods in Java RecursiveAction

Page 31: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

31

• RecursiveTask extends ForkJoinTaskto return a result

• compute() must be overriddenby subclasses to perform thetask’s main computation

• It may split its work up into smaller sub-tasks that are fork()’d to run in parallel

abstract class RecursiveAction

extends ForkJoinTask<Void> {

protected abstract Void

compute();

...

Key Methods in Java RecursiveAction

Page 32: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

32

• RecursiveTask extends ForkJoinTaskto return a result

• compute() must be overriddenby subclasses to perform thetask’s main computation

• It may split its work up into smaller sub-tasks that are fork()’d to run in parallel

• It join()’s these smaller sub-tasks but does not return aresult directly

• Result’s often stored in array

abstract class RecursiveAction

extends ForkJoinTask<Void> {

protected abstract Void

compute();

...

Key Methods in Java RecursiveAction

Page 33: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

33

• RecursiveTask extends ForkJoinTaskto return a result

• compute() must be overriddenby subclasses to perform thetask’s main computation

• Called internally by the fork-join pool to execute the task

abstract class RecursiveAction

extends ForkJoinTask<Void> {

protected abstract Void

compute();

protected final boolean exec(){

compute();

return true;

}

...

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/RecursiveTask.html#exec

Key Methods in Java RecursiveAction

Page 34: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

34

Key Methods in the Java RecursiveTask

Page 35: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

35

• RecursiveTask extends ForkJoinTaskto return a result

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/RecursiveTask.html

Key Methods in Java RecursiveTaskabstract class RecursiveTask<V>

extends ForkJoinTask<V> {

...

Page 36: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

36

• RecursiveTask extends ForkJoinTaskto return a result

• compute() must be overriddenby subclasses to perform thetask’s main computation

abstract class RecursiveTask<V>

extends ForkJoinTask<V> {

protected abstract V

compute();

...

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/RecursiveTask.html#compute

Key Methods in Java RecursiveTask

Page 37: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

37

• RecursiveTask extends ForkJoinTaskto return a result

• compute() must be overriddenby subclasses to perform thetask’s main computation

• It may split its work up into smaller sub-tasks that are fork()’d to run in parallel

abstract class RecursiveTask<V>

extends ForkJoinTask<V> {

protected abstract V

compute();

...

Key Methods in Java RecursiveTask

Page 38: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

38

• RecursiveTask extends ForkJoinTaskto return a result

• compute() must be overriddenby subclasses to perform thetask’s main computation

• It may split its work up into smaller sub-tasks that are fork()’d to run in parallel

• It join()’s the results of these smaller sub-tasks into a collective result

abstract class RecursiveTask<V>

extends ForkJoinTask<V> {

protected abstract V

compute();

...

Key Methods in Java RecursiveTask

Page 39: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

39

• RecursiveTask extends ForkJoinTaskto return a result

• compute() must be overriddenby subclasses to perform thetask’s main computation

• Called internally by the fork-join pool to execute the task

abstract class RecursiveTask<V>

extends ForkJoinTask<V> {

protected abstract V

compute();

V result;

protected final boolean exec(){

result = compute();

return true;

}

...

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/RecursiveTask.html#exec

Key Methods in Java RecursiveTask

Page 40: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods

40

End of the Java Fork-Join

Pool Framework (Part 4)