overview of basic java 8 completablefuture features (part 2)schmidt/cs891f/2018-pdfs/...8 applying...
TRANSCRIPT
![Page 1: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/1.jpg)
Overview of Basic Java 8
CompletableFuture Features (Part 2)
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 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/2.jpg)
2
• Understand the basic completablefutures features
• Know how to apply these basic features to operate on big fractions
Learning Objectives in this Part of the Lesson
See github.com/douglascraigschmidt/LiveLessons/tree/master/Java8/ex8
![Page 3: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/3.jpg)
3
• Understand the basic completablefutures features
• Know how to apply these basic features to operate on big fractions
• Recognize limitations with these basic features
Learning Objectives in this Part of the Lesson
![Page 4: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/4.jpg)
4
Applying Basic CompletableFuture Features
![Page 5: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/5.jpg)
5
Applying Basic Completable Future Features• We show how to apply basic completable
future features in the context of BigFraction
See LiveLessons/blob/master/Java8/ex8/src/utils/BigFraction.java
![Page 6: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/6.jpg)
6
Applying Basic Completable Future Features• We show how to apply basic completable
future features in the context of BigFraction
• Arbitrary-precision fraction, utilizing BigIntegers for numerator & denominator
![Page 7: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/7.jpg)
7
Applying Basic Completable Future Features• We show how to apply basic completable
future features in the context of BigFraction
• Arbitrary-precision fraction, utilizing BigIntegers for numerator & denominator
• Factory methods for creating “reduced”fractions, e.g.
• 44/55 → 4/5
• 12/24 → 1/2
• 144/216 → 2/3
![Page 8: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/8.jpg)
8
Applying Basic Completable Future Features• We show how to apply basic completable
future features in the context of BigFraction
• Arbitrary-precision fraction, utilizing BigIntegers for numerator & denominator
• Factory methods for creating “reduced”fractions
• Factory methods for creating “non-reduced” fractions (& then reducing them)
• e.g., 12/24 (→ 1/2)
![Page 9: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/9.jpg)
9
Applying Basic Completable Future Features• We show how to apply basic completable
future features in the context of BigFraction
• Arbitrary-precision fraction, utilizing BigIntegers for numerator & denominator
• Factory methods for creating “reduced”fractions
• Factory methods for creating “non-reduced” fractions (& then reducing them)
• Arbitrary-precision fraction arithmetic
• e.g., 18/4 ×2/3 = 3
![Page 10: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/10.jpg)
10
Applying Basic Completable Future Features• We show how to apply basic completable
future features in the context of BigFraction
• Arbitrary-precision fraction, utilizing BigIntegers for numerator & denominator
• Factory methods for creating “reduced”fractions
• Factory methods for creating “non-reduced” fractions (& then reducing them)
• Arbitrary-precision fraction arithmetic
• Create a mixed fraction from an improperfraction
• e.g., 18/4 → 4 1/2
![Page 11: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/11.jpg)
11
Applying Basic Completable Future Features
CompletableFuture<BigFraction> future
= new CompletableFuture<>();
new Thread (() -> {
BigFraction bf1 =
new BigFraction("62675744/15668936");
BigFraction bf2 =
new BigFraction("609136/913704");
future.complete(bf1.multiply(bf2));
}).start();
...
System.out.println(future.join().toMixedString());
• Multiplying big fractions w/a completable future : Main
start()
…
new()
: Completable
Future
: Backround
Thread
complete()
join()
…
new()
See github.com/douglascraigschmidt/LiveLessons/tree/master/Java8/ex8
![Page 12: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/12.jpg)
12
Applying Basic Completable Future Features
CompletableFuture<BigFraction> future
= new CompletableFuture<>();
new Thread (() -> {
BigFraction bf1 =
new BigFraction("62675744/15668936");
BigFraction bf2 =
new BigFraction("609136/913704");
future.complete(bf1.multiply(bf2));
}).start();
...
System.out.println(future.join().toMixedString());
Make “empty” future
• Multiplying big fractions w/a completable future
start()
…
new()
: Completable
Future
: Backround
Thread
complete()
join()
…
new()
: Main
![Page 13: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/13.jpg)
13
Applying Basic Completable Future Features
CompletableFuture<BigFraction> future
= new CompletableFuture<>();
new Thread (() -> {
BigFraction bf1 =
new BigFraction("62675744/15668936");
BigFraction bf2 =
new BigFraction("609136/913704");
future.complete(bf1.multiply(bf2));
}).start();
...
System.out.println(future.join().toMixedString());
Start computation in a background thread
• Multiplying big fractions w/a completable future
start()
…
new()
: Completable
Future
: Backround
Thread
complete()
join()
…
new()
: Main
![Page 14: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/14.jpg)
14
Applying Basic Completable Future Features
CompletableFuture<BigFraction> future
= new CompletableFuture<>();
new Thread (() -> {
BigFraction bf1 =
new BigFraction("62675744/15668936");
BigFraction bf2 =
new BigFraction("609136/913704");
future.complete(bf1.multiply(bf2));
}).start();
...
System.out.println(future.join().toMixedString());
• Multiplying big fractions w/a completable future
start()
…
new()
: Completable
Future
: Backround
Thread
complete()
join()
…
new()
The computation multiplies BigFractions (via BigIntegers)
: Main
See docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html
![Page 15: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/15.jpg)
15
Applying Basic Completable Future Features
CompletableFuture<BigFraction> future
= new CompletableFuture<>();
new Thread (() -> {
BigFraction bf1 =
new BigFraction("62675744/15668936");
BigFraction bf2 =
new BigFraction("609136/913704");
future.complete(bf1.multiply(bf2));
}).start();
...
System.out.println(future.join().toMixedString());
• Multiplying big fractions w/a completable future
These computations run concurrently
start()
…
new()
: Completable
Future
: Backround
Thread
complete()
join()
…
new()
: Main
![Page 16: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/16.jpg)
16
Applying Basic Completable Future Features
CompletableFuture<BigFraction> future
= new CompletableFuture<>();
new Thread (() -> {
BigFraction bf1 =
new BigFraction("62675744/15668936");
BigFraction bf2 =
new BigFraction("609136/913704");
future.complete(bf1.multiply(bf2));
}).start();
...
System.out.println(future.join().toMixedString());
• Multiplying big fractions w/a completable future
Explicitly complete the future w/result
start()
…
new()
: Completable
Future
: Backround
Thread
complete()
join()
…
new()
: Main
![Page 17: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/17.jpg)
17
Applying Basic Completable Future Features
CompletableFuture<BigFraction> future
= new CompletableFuture<>();
new Thread (() -> {
BigFraction bf1 =
new BigFraction("62675744/15668936");
BigFraction bf2 =
new BigFraction("609136/913704");
future.complete(bf1.multiply(bf2));
}).start();
...
System.out.println(future.join().toMixedString());
• Multiplying big fractions w/a completable future
join() blocks until result is computed
start()
…
new()
: Completable
Future
: Backround
Thread
complete()
join()
…
new()
: Main
![Page 18: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/18.jpg)
18
Limitations with Basic Completable Futures Features
![Page 19: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/19.jpg)
19
Limitations with Basic Completable Futures Features• Basic completable future features
have similar limitations as futures
• Cannot be chained fluently to handle async results
• Cannot be triggered reactively
• Cannot be treated efficientlyas a collection of futures
![Page 20: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/20.jpg)
20
Limitations with Basic Completable Futures Features
CompletableFuture<BigFraction> future
= new CompletableFuture<>();
new Thread (() -> {
BigFraction bf1 =
new BigFraction("62675744/15668936");
BigFraction bf2 =
new BigFraction("609136/913704");
future.complete(bf1.multiply(bf2));
}).start();
...
System.out.println(future.join().toMixedString());
• e.g., join() blocks until the future is completed..
This blocking call underutilizes cores & increases overhead
![Page 21: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/21.jpg)
21See crondev.blog/2017/01/23/timeouts-with-java-8-completablefuture-youre-probably-doing-it-wrong
Limitations with Basic Completable Futures Features
CompletableFuture<BigFraction> future
= new CompletableFuture<>();
new Thread (() -> {
BigFraction bf1 =
new BigFraction("62675744/15668936");
BigFraction bf2 =
new BigFraction("609136/913704");
future.complete(bf1.multiply(bf2));
}).start();
...
System.out.println(future.join(1, SECONDS).toMixedString());
• e.g., join() blocks until the future is completed..
Using a timeout to bound the blocking duration is inefficient & error-prone
![Page 22: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/22.jpg)
22
Limitations with Basic Completable Futures Features• We therefore need to leverage the
advanced features of completablefutures
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html
![Page 23: Overview of Basic Java 8 CompletableFuture Features (Part 2)schmidt/cs891f/2018-PDFs/...8 Applying Basic Completable Future Features •We show how to apply basic completable future](https://reader034.vdocuments.us/reader034/viewer/2022043010/5f9ff66393aca74f582d80cf/html5/thumbnails/23.jpg)
23
End of Overview of Basic Java 8 CompletableFuture Features (Part 2)