15
Overview of Basic Java 8 CompletableFuture Features (Part 1) Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Overview of Basic Java 8 CompletableFuture Features (Part 1)schmidt/cs891f/2018-PDFs/07... · 2018-11-05 · 2 Learning Objectives in this Part of the Lesson •Understand the basic

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Overview of Basic Java 8 CompletableFuture Features (Part 1)schmidt/cs891f/2018-PDFs/07... · 2018-11-05 · 2 Learning Objectives in this Part of the Lesson •Understand the basic

Overview of Basic Java 8

CompletableFuture Features (Part 1)

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: Overview of Basic Java 8 CompletableFuture Features (Part 1)schmidt/cs891f/2018-PDFs/07... · 2018-11-05 · 2 Learning Objectives in this Part of the Lesson •Understand the basic

2

Learning Objectives in this Part of the Lesson• Understand the basic completable

futures features

Page 3: Overview of Basic Java 8 CompletableFuture Features (Part 1)schmidt/cs891f/2018-PDFs/07... · 2018-11-05 · 2 Learning Objectives in this Part of the Lesson •Understand the basic

3

Basic CompletableFuture Features

Page 4: Overview of Basic Java 8 CompletableFuture Features (Part 1)schmidt/cs891f/2018-PDFs/07... · 2018-11-05 · 2 Learning Objectives in this Part of the Lesson •Understand the basic

4

• Basic completable future features

Basic Completable Future Features

: Main

start()

new()

: Completable

Future

: Backround

Thread

complete()

join()

new()

See github.com/douglascraigschmidt/LiveLessons/tree/master/Java8/ex8

Page 5: Overview of Basic Java 8 CompletableFuture Features (Part 1)schmidt/cs891f/2018-PDFs/07... · 2018-11-05 · 2 Learning Objectives in this Part of the Lesson •Understand the basic

5

• Basic completable future features

• Support the Future API

Basic Completable Future Features

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

Page 6: Overview of Basic Java 8 CompletableFuture Features (Part 1)schmidt/cs891f/2018-PDFs/07... · 2018-11-05 · 2 Learning Objectives in this Part of the Lesson •Understand the basic

6

• Basic completable future features

• Support the Future API

• Can (time-) block & poll

Basic Completable Future Features

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

String f1 = "62675744/15668936";

String f2 = "609136/913704";

ForkJoinTask<BigFraction> f =

commonPool().submit(() -> {

BigFraction bf1 =

new BigFraction(f1);

BigFraction bf2 =

new BigFraction(f2);

return bf1.multiply(bf2);

});

...

BigFraction result = f.get();

// f.get(10, MILLISECONDS);

// f.get(0, 0);

Page 7: Overview of Basic Java 8 CompletableFuture Features (Part 1)schmidt/cs891f/2018-PDFs/07... · 2018-11-05 · 2 Learning Objectives in this Part of the Lesson •Understand the basic

7

• Basic completable future features

• Support the Future API

• Can (time-) block & poll

• Can be cancelled & tested if canceled/done

Basic Completable Future Features

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

String f1 = "62675744/15668936";

String f2 = "609136/913704";

ForkJoinTask<BigFraction> f =

commonPool().submit(() -> {

BigFraction bf1 =

new BigFraction(f1);

BigFraction bf2 =

new BigFraction(f2);

return bf1.multiply(bf2);

});

...

if (!(f.isDone()

|| !f.isCancelled()))

f.cancel();

Page 8: Overview of Basic Java 8 CompletableFuture Features (Part 1)schmidt/cs891f/2018-PDFs/07... · 2018-11-05 · 2 Learning Objectives in this Part of the Lesson •Understand the basic

8

• Basic completable future features

• Support the Future API

• Can (time-) block & poll

• Can be cancelled & tested if canceled/done

• cancel() doesn’t interrupt the computation by default..

Basic Completable Future Features

See www.nurkiewicz.com/2015/03/completablefuture-cant-be-interrupted.html

String f1 = "62675744/15668936";

String f2 = "609136/913704";

ForkJoinTask<BigFraction> f =

commonPool().submit(() -> {

BigFraction bf1 =

new BigFraction(f1);

BigFraction bf2 =

new BigFraction(f2);

return bf1.multiply(bf2);

});

...

if (!(f.isDone()

|| !f.isCancelled()))

f.cancel();

Page 9: Overview of Basic Java 8 CompletableFuture Features (Part 1)schmidt/cs891f/2018-PDFs/07... · 2018-11-05 · 2 Learning Objectives in this Part of the Lesson •Understand the basic

9

• Basic completable future features

• Support the Future API

• Define a join() method

Basic Completable Future Features

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

Page 10: Overview of Basic Java 8 CompletableFuture Features (Part 1)schmidt/cs891f/2018-PDFs/07... · 2018-11-05 · 2 Learning Objectives in this Part of the Lesson •Understand the basic

10

• Basic completable future features

• Support the Future API

• Define a join() method

• Behaves like get() withoutusing checked exceptions

Basic Completable Future Features

futures

.stream()

.map(Future::join)

.collect(toList())

Future::join can be used as a method reference in a Java 8 stream

Page 11: Overview of Basic Java 8 CompletableFuture Features (Part 1)schmidt/cs891f/2018-PDFs/07... · 2018-11-05 · 2 Learning Objectives in this Part of the Lesson •Understand the basic

11

futures

.stream()

.map(future

-> try { future.get();

} catch (Exception e){

})

.collect(toList())

• Basic completable future features

• Support the Future API

• Define a join() method

• Behaves like get() withoutusing checked exceptions

Basic Completable Future Features

Mixing checked exceptions & Java 8 streams is ugly..

Page 12: Overview of Basic Java 8 CompletableFuture Features (Part 1)schmidt/cs891f/2018-PDFs/07... · 2018-11-05 · 2 Learning Objectives in this Part of the Lesson •Understand the basic

12

• Basic completable future features

• Support the Future API

• Define a join() method

• Can be completed explicitly

Basic Completable Future Features

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

Page 13: Overview of Basic Java 8 CompletableFuture Features (Part 1)schmidt/cs891f/2018-PDFs/07... · 2018-11-05 · 2 Learning Objectives in this Part of the Lesson •Understand the basic

13

• Basic completable future features

• Support the Future API

• Define a join() method

• Can be completed explicitly

• i.e., sets result returned byget()/join() to a given value

Basic Completable Future Features

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

CompletableFuture<...> future =

new CompletableFuture<>();

new Thread (() -> {

...

future.complete(...);

}).start();

...

System.out.println(future.join());

After complete() is done calls to join() will unblock

Page 14: Overview of Basic Java 8 CompletableFuture Features (Part 1)schmidt/cs891f/2018-PDFs/07... · 2018-11-05 · 2 Learning Objectives in this Part of the Lesson •Understand the basic

14

• Basic completable future features

• Support the Future API

• Define a join() method

• Can be completed explicitly

• i.e., sets result returned byget()/join() to a given value

Basic Completable Future Features

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

CompletableFuture<...> future =

new CompletableFuture<>();

final CompletableFuture<Long> zero

= CompletableFuture

.completedFuture(0L);

new Thread (() -> {

...

future.complete(zero.join());

}).start();

...

System.out.println(future.join());

A completable future can be initialized to a value/constant

Page 15: Overview of Basic Java 8 CompletableFuture Features (Part 1)schmidt/cs891f/2018-PDFs/07... · 2018-11-05 · 2 Learning Objectives in this Part of the Lesson •Understand the basic

15

End of Overview of Basic Java 8 CompletableFuture Features (Part 1)