Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2232fc1

Browse filesBrowse files
author
Guruprasad Kulkarni
committed
Further updates
1 parent 83ff441 commit 2232fc1
Copy full SHA for 2232fc1

File tree

Expand file treeCollapse file tree

1 file changed

+20
-11
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+20
-11
lines changed

‎com/linux/java/se/nine/additions/CompletableFutureAdditions.java

Copy file name to clipboardExpand all lines: com/linux/java/se/nine/additions/CompletableFutureAdditions.java
+20-11Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,48 @@
1111
*/
1212
public class CompletableFutureAdditions {
1313
public static void main(String[] args) {
14-
CompletableFutureAdditions additions = new CompletableFutureAdditions();
14+
CompletableFutureAdditions cfAdditions = new CompletableFutureAdditions();
1515

1616
System.out.println("Before First CompletableFuture " + currentTimeMillis());
17-
CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> additions.sleep(2));
17+
CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> cfAdditions.sleep(2));
1818
System.out.printf("Original Completable Future is done ? %b%n%n", cf.isDone());
1919

20-
System.out.println("Before a copy of Completable Future " + currentTimeMillis());
20+
System.out.println("Before creating a copy of Completable Future " + currentTimeMillis());
2121
CompletableFuture<String> newCopy = cf.copy();
22-
System.out.printf("Copy of Completable Future is done ? %b%n%n", cf.isDone());
23-
newCopy.completeExceptionally(new InterruptedException("newCopy :: Some message"));
22+
System.out.printf("Copy of Completable Future is done ? %b%n%n", newCopy.isDone());
23+
24+
25+
System.out.println("Before a copy of Completable Future for completing exceptionally " + currentTimeMillis());
26+
CompletableFuture<String> newCopyForCompleteExceptionally = cf.copy();
27+
System.out.printf("Copy of Completable Future for completing exceptionally is done ? %b%n%n", newCopyForCompleteExceptionally.isDone());
28+
newCopyForCompleteExceptionally.completeExceptionally(new InterruptedException("newCopyForCompleteExceptionally :: Some message"));
2429

2530
System.out.println("Before Copying and completing on timeout " + currentTimeMillis());
2631
CompletableFuture<String> completeOnTimeOut = cf.copy().completeOnTimeout("completeOnTimeOut :: Failed to complete", 1, TimeUnit.SECONDS);
27-
System.out.printf("Completable Future Complete on Timeout is done ? %b%n%n", cf.isDone());
32+
System.out.printf("Completable future complete on timeout is done ? %b%n%n", completeOnTimeOut.isDone());
2833

29-
cf.thenAccept(s -> System.out.printf("Result after Original Future Completion : %s%n%n", s));
34+
cf.thenAccept(s -> System.out.printf("%nResult after Original future completion : %s%n%n", s));
35+
36+
newCopy.thenAccept(s -> System.out.printf("%nResult after Original future completion : %s%n%n", s));
3037

31-
newCopy.thenAccept(s -> System.out.printf("Result after Copied Future Completion : %s%n%n", s));
38+
newCopyForCompleteExceptionally.thenAccept(s -> System.out.printf("%nResult after Copied future Completion : %s%n%n", s));
3239

33-
completeOnTimeOut.thenAccept(s -> System.out.printf("Result after Copied and Timed out Future Completion : %s%n%n", s));
40+
completeOnTimeOut.thenAccept(s -> System.out.printf("%nResult after Copied and timed out future Completion : %s%n%n", s));
3441

35-
System.out.println("Before Final Sleep " + currentTimeMillis());
36-
additions.sleep(5);
42+
System.out.println("Before Final Sleep -- this is needed otherwise we will see no output. " + currentTimeMillis());
43+
cfAdditions.sleep(5);
3744

3845
}
3946

4047

4148
private String sleep(int sleepTimeSeconds) {
49+
System.out.println(Thread.currentThread().getId() + " is sleeping ... for " + sleepTimeSeconds);
4250
try {
4351
Thread.sleep(TimeUnit.SECONDS.toMillis(sleepTimeSeconds));
4452
} catch (InterruptedException e) {
4553
throw new RuntimeException(e);
4654
}
55+
System.out.println("Waking up! ... after " + sleepTimeSeconds);
4756
return "Done : " + currentTimeMillis() + " By " + Thread.currentThread().getName();
4857
}
4958

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.