Skip to content

Commit 1fab573

Browse files
Removed CompletionStage details
1 parent ab9df34 commit 1fab573

1 file changed

Lines changed: 5 additions & 42 deletions

File tree

‎topics/core/java-8.md‎

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
* [Examples:](#examples-)
55
* [Comparators](#comparators)
66
* [Concurrency](#concurrency)
7-
* [CompletionStage](#completionstage)
8-
* [CompletableFuture (= Future + CompletionStage)](#completablefuture----future---completionstage-)
7+
* [CompletableFuture](#completablefuture)
98
* [StampedLock](#stampedlock)
109
* [@Contended](#-contended)
1110

@@ -146,19 +145,9 @@ Operations
146145
- sum // retrieves result by coordinating between threads
147146
- reset
148147

148+
### CompletableFuture
149149

150-
### CompletionStage
151-
152-
```java
153-
stage.thenApply(x -> square(x))
154-
.thenAccept(x -> System.out.println(x))
155-
.thenRun(() -> System.out.println(“done”));
156-
```
157-
Each of the above calls have async variant
158-
159-
### CompletableFuture (= Future + CompletionStage)
160-
161-
Basically provide a call back to the future itself, so that as soon the result is available it can react to it by triggering those callbacks. In ```future``` the method ```get``` is blocking (waits indefinitely for the result). In completableFuture the current thread is not blocked.
150+
Similar to JavaScript promises. Multiple async tasks can be chained (performed one after another on separate thread). Better alternative to ```future``` where the method ```get``` is blocking (waits indefinitely for the result). In completableFuture the current thread is not blocked. Each task is executed once previous task is completed.
162151

163152
Also, the program itself becomes more readable.
164153

@@ -170,7 +159,7 @@ Also, the program itself becomes more readable.
170159
.thenRun(() -> System.out.println(“done”)));
171160
```
172161

173-
So when you trigger this, it immediately returns the CompletableFuture, and you can check its methods below to check status and such.
162+
So when you trigger this, it immediately returns the CompletableFuture instance, which can be used to check its status and such.
174163

175164
**Massive API**
176165

@@ -180,35 +169,9 @@ So when you trigger this, it immediately returns the CompletableFuture, and you
180169
- cancel()
181170
- isDone()
182171

183-
**Producer of the CompletableFuture**
184-
185-
So you return a new CompletableFuture immediately, and keep its reference so that while processing and after processing you can trigger the right methods on it (complete)
186-
187-
```java
188-
CompletionStage<String> getWebpage(URL url){
189-
190-
CompletableFuture<String> future = new CompletableFuture<>();
191-
192-
Runnable task = new Runnable(){
193-
public void run(){
194-
try {
195-
String webpage = blockingReadWebpage(url); // produce result
196-
future.complete(webpage); // set result
197-
} catch(Exception e){
198-
future.completeExceptionally(e); //set exception
199-
}
200-
}
201-
}
202-
203-
pool.execute(task);
204-
return future;
205-
}
206-
```
207-
208-
209172
### StampedLock
210-
211173
- Better alternative for ReadWriteLock
174+
212175
- It does optimistic reads so works faster only on less contended operations
213176
- Not re-entrant
214177

0 commit comments

Comments
 (0)