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 2d51362

Browse filesBrowse files
author
Guruprasad Kulkarni
committed
Pretty printing
1 parent d6a0796 commit 2d51362
Copy full SHA for 2d51362

File tree

Expand file treeCollapse file tree

2 files changed

+23
-14
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+23
-14
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
+19-10Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,36 @@
33
import java.util.concurrent.CompletableFuture;
44
import java.util.concurrent.TimeUnit;
55

6+
import static java.lang.System.currentTimeMillis;
7+
8+
69
/**
710
* CompletableFutureAdditions
811
*/
912
public class CompletableFutureAdditions {
1013
public static void main(String[] args) {
1114
CompletableFutureAdditions additions = new CompletableFutureAdditions();
15+
16+
System.out.println("Before First CompletableFuture " + currentTimeMillis());
1217
CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> additions.sleep(2));
13-
System.out.println(" Original Completable Future is done ?" + cf.isDone());
14-
18+
System.out.printf("Original Completable Future is done ? %b%n%n", cf.isDone());
19+
20+
System.out.println("Before a copy of Completable Future " + currentTimeMillis());
1521
CompletableFuture<String> newCopy = cf.copy();
16-
System.out.println(" Copy of Completable Future is done ?" + cf.isDone());
17-
newCopy.completeExceptionally(new InterruptedException("Some message"));
22+
System.out.printf("Copy of Completable Future is done ? %b%n%n", cf.isDone());
23+
newCopy.completeExceptionally(new InterruptedException("newCopy :: Some message"));
24+
25+
System.out.println("Before Copying and completing on timeout " + currentTimeMillis());
26+
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());
1828

19-
CompletableFuture<String> completeOnTimeOut = cf.copy().completeOnTimeout("Failed to complete", 1, TimeUnit.SECONDS);
20-
System.out.println("Completable Future Complete on Timeout is done ?" + cf.isDone());
29+
cf.thenAccept(s -> System.out.printf("Result after Original Future Completion : %s%n%n", s));
2130

22-
cf.thenAccept(System.out::println);
23-
newCopy.thenAccept(System.out::println);
24-
completeOnTimeOut.thenAccept(System.out::println);
31+
newCopy.thenAccept(s -> System.out.printf("Result after Copied Future Completion : %s%n%n", s));
2532

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

35+
System.out.println("Before Final Sleep " + currentTimeMillis());
2736
additions.sleep(5);
2837

2938
}
@@ -35,7 +44,7 @@ private String sleep(int sleepTimeSeconds) {
3544
} catch (InterruptedException e) {
3645
throw new RuntimeException(e);
3746
}
38-
return "Done : " + System.currentTimeMillis() + " By " + Thread.currentThread().getName();
47+
return "Done : " + currentTimeMillis() + " By " + Thread.currentThread().getName();
3948
}
4049

4150
}

‎com/linux/java/se/nine/improvements/TryWithResources.java

Copy file name to clipboardExpand all lines: com/linux/java/se/nine/improvements/TryWithResources.java
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ public String doWork(String context) {
1515
} catch(InterruptedException ie) {
1616
throw new RuntimeException(ie);
1717
}
18-
return "Work Done -- " + context;
18+
return "from doWork : Work Done -- " + context;
1919
}
2020

2121
@Override
2222
public void close() {
23-
System.out.printf("%nResource %s closed%n%n", name);
23+
System.out.printf("from close : Resource %s closed%n", name);
2424
}
2525
}
2626

@@ -29,14 +29,14 @@ public void close() {
2929
*/
3030
public class TryWithResources {
3131
public static void main(String[] args) {
32-
System.out.printf("%n-- Before :: %n");
32+
System.out.printf("%n-- Before :: %n%n");
3333
try(SimpleResource res = new SimpleResource("Inside Try With Resources")){
3434
String workResponse = res.doWork("old style");
3535
System.out.println(workResponse);
3636
}
3737

3838

39-
System.out.printf("%n-- After :: %n");
39+
System.out.printf("%n-- After :: %n%n");
4040
SimpleResource resource = new SimpleResource("Outside Try With Resources");
4141
try(resource) {
4242
String workResponse = resource.doWork("Java 9 Style");

0 commit comments

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