• No results found

Evaluating the Pros of the Java Completable Futures Framework

N/A
N/A
Protected

Academic year: 2021

Share "Evaluating the Pros of the Java Completable Futures Framework"

Copied!
19
0
0

Loading.... (view fulltext now)

Full text

(1)

Evaluating the Pros of the Java

Completable Futures Framework

Douglas C. Schmidt

[email protected]

www.dre.vanderbilt.edu/~schmidt

Professor of Computer Science Institute for Software

(2)

2

Learning Objectives in this Part of the Lesson

(3)

3

• Evaluate the pros of using the Java completable futures framework

• We evaluate the Java completable futures framework compared with the parallel streams framework

See github.com/douglascraigschmidt/LiveLessons/tree/master/ImageStreamGang Completable Futuresmap(this::downloadImageAsync) thenAccept(this::logResults) flatMap(this::applyFiltersAsync) collect(toFuture()) map(this::checkUrlCachedAsync) collect(toList()) Parallel Streamsfilter(not(this::urlCached)) map(this::downloadImage) flatMap(this::applyFilters)

(4)

4

(5)

5

Pros of the Java Completable Futures Framework

• Greatly simplifies programming of asynchronous operations

/page\ = supplyAsync (getStartPage()) /imgNum2\ = /page\ .thenComposeAsync (crawlHyperLinks (page)) /imgNum1\ = /page\ .thenApplyAsync (countImages(page)) .thenApply(List::size) /imgNum1\.thenCombine(/imgNum2\,

(imgNum1, imgNum2) -> Integer::sum)

Task 1

Task 2 Task 3

(6)

6

Pros of the Java Completable Futures Framework

• Greatly simplifies programming of asynchronous operations • Supports dependent actions

that trigger upon completion of async operations /page\ = supplyAsync (getStartPage()) /imgNum2\ = /page\ .thenComposeAsync (crawlHyperLinks (page)) /imgNum1\ = /page\ .thenApplyAsync (countImages(page)) .thenApply(List::size)

/imgNum1\.thenCombine(/imgNum2\,

(imgNum1, imgNum2) -> Integer::sum)

Task 1

Task 2 Task 3

(7)

7

Pros of the Java Completable Futures Framework

• Greatly simplifies programming of asynchronous operations • Supports dependent actions

that trigger upon completion of async operations

• Async operations can be forked, chained, & joined in a relatively intuitive way

/page\ = supplyAsync (getStartPage()) /imgNum2\ = /page\ .thenComposeAsync (crawlHyperLinks (page)) /imgNum1\ = /page\ .thenApplyAsync (countImages(page)) .thenApply(List::size)

/imgNum1\.thenCombine(/imgNum2\,

(imgNum1, imgNum2) -> Integer::sum)

Task 1

Task 2 Task 3

(8)

8

Pros of the Java Completable Futures Framework

• Greatly simplifies programming of asynchronous operations • Supports dependent actions

that trigger upon completion of async operations

• Async operations can be forked, chained, & joined in a relatively intuitive way

• Enables async programs to appear like sync programs

BigFraction unreduced = BigFraction .valueOf(new BigInteger

("846122553600669882"), new BigInteger

(9)

9

Pros of the Java Completable Futures Framework

• Greatly simplifies programming of asynchronous operations

• Supports dependent actions that trigger upon completion of async operations

• Async operations run in parallel in a thread pool

/page\ = supplyAsync (getStartPage()) /imgNum2\ = /page\ .thenComposeAsync (crawlHyperLinks (page)) /imgNum1\ = /page\ .thenApplyAsync (countImages(page)) .thenApply(List::size)

/imgNum1\.thenCombine(/imgNum2\,

(imgNum1, imgNum2) -> Integer::sum)

Task 1

Task 2 Task 3

(10)

10

Pros of the Java Completable Futures Framework

• Greatly simplifies programming of asynchronous operations

• Supports dependent actions that trigger upon completion of async operations

• Async operations run in parallel in a thread pool

• Either a (common) fork-join pool or various types of pre-or user-defined thread pools

/page\ = supplyAsync (getStartPage()) /imgNum2\ = /page\ .thenComposeAsync (crawlHyperLinks (page)) /imgNum1\ = /page\ .thenApplyAsync (countImages(page)) .thenApply(List::size)

/imgNum1\.thenCombine(/imgNum2\,

(imgNum1, imgNum2) -> Integer::sum)

Task 1

Task 2 Task 3

(11)

11

Pros of the Java Completable Futures Framework

• No explicit synchronization or threading is required for completable futures

(12)

12

See docs.oracle.com/javase/tutorial/essential/concurrency/collections.html

Pros of the Java Completable Futures Framework

Completable Futures

map(this::downloadImageAsync) thenAccept(this::logResults) flatMap(this::applyFiltersAsync) collect(toFuture()) map(this::checkUrlCachedAsync)

• No explicit synchronization or threading is required for completable futures • Java libraries handle locking needed

(13)

13

Pros of the Java Completable Futures Framework

Printing 4 results for input file 1 from fastest to slowest

COMPLETABLE_FUTURES_1 executed in 312 msecs COMPLETABLE_FUTURES_2 executed in 335 msecs PARALLEL_STREAM executed in 428 msecs

SEQUENTIAL_STREAM executed in 981 msecs

Printing 4 results for input file 2 from fastest to slowest

COMPLETABLE_FUTURES_2 executed in 82 msecs COMPLETABLE_FUTURES_1 executed in 83 msecs PARALLEL_STREAM executed in 102 msecs

SEQUENTIAL_STREAM executed in 251 msecs

• Completable futures are often more efficient than parallel streams

(14)

14

There’s no substitute for benchmarking, e.g., java-performance.info/jmh!

Pros of the Java Completable Futures Framework

• Completable futures are often more efficient than parallel streams • Naturally, your mileage may vary.. Completable Futures

(15)

15

Pros of the Java Completable Futures Framework

• Combining sequential streams & completable futures is often a win

(16)

16

Pros of the Java Completable Futures Framework

• Combining sequential streams & completable futures is often a win • Streams guide the overall flow of

(17)

17

Pros of the Java Completable Futures Framework

• Combining sequential streams & completable futures is often a win

• Streams guide the overall flow of control & completable futures

(18)

18

Pros of the Java Completable Futures Framework

• Combining sequential streams & completable futures is often a win

• Streams guide the overall flow of control & completable futures

perform perform async operations efficiently in parallel.

(19)

19

End of Evaluating the Pros

of the Java Completable

References

Related documents

Key words: Ahtna Athabascans, Community Subsistence Harvest, subsistence hunting, GMU 13 moose, Alaska Board o f Game, Copper River Basin, natural resource management,

All stationary perfect equilibria of the intertemporal game approach (as slight stochastic perturbations as in Nash (1953) tend to zero) the same division of surplus as the static

FX: •INR/USD Futures •EUR/USD Futures •GBP/USD Futures •JPY/USD Futures •AUD/USD Futures •CAD/USD Futures •CHF/USD Futures... • The global FX market has grown

- Futures and forward contracts (Characteristics of forward and futures contracts, Mechanics of trading in futures markets, Various futures contracts, Futures valuation

Options on Phelix* Power Futures Bas e Month Futures Quarter Futures Year Futures. Option products

By first analysing the image data in terms of the local image structures, such as lines or edges, and then controlling the filtering based on local information from the analysis

Working in partnership with the trust’s centralised staffing solutions (CSS) call centre team, the e-rostering team and the ward managers, Allocate software was used to help

The expected learning outcomes will be determined by crossing the learner’s prior knowledge assessment with the required competences for a given professional profile and level..