pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/mil-lion/HelloJavaSE/commit/babc3df9a8489671fc54548784226bb85face66e

href="https://github.githubassets.com/assets/global-d18f184ea1a06a2c.css" /> Добавлены примеры по работе с классоим CompletableFuture · mil-lion/HelloJavaSE@babc3df · GitHub
Skip to content

Commit babc3df

Browse files
committed
Добавлены примеры по работе с классоим CompletableFuture
1 parent 2bedb0a commit babc3df

1 file changed

Lines changed: 40 additions & 1 deletion

File tree

src/main/java/ru/lionsoft/javase/hello/thread/HelloFuture.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
package ru.lionsoft.javase.hello.thread;
1010

1111
import java.util.concurrent.Callable;
12+
import java.util.concurrent.CompletableFuture;
1213
import java.util.concurrent.FutureTask;
14+
import java.util.concurrent.TimeUnit;
15+
import java.util.concurrent.TimeoutException;
1316

1417
/**
1518
* Пример работы с Callable интерфейсом и асинхронное выполнение функции
@@ -18,6 +21,12 @@
1821
public class HelloFuture {
1922

2023
public static void main(String[] args) throws Exception {
24+
25+
testFutureTask();
26+
testCompletableFuture();
27+
}
28+
29+
public static void testFutureTask() throws Exception {
2130
Callable<String> task = () -> {
2231
try {
2332
Thread.sleep(500);
@@ -27,6 +36,36 @@ public static void main(String[] args) throws Exception {
2736
};
2837
FutureTask<String> future = new FutureTask<>(task);
2938
new Thread(future).start();
30-
System.out.println(future.get());
39+
String hello = null;
40+
int counter = 0;
41+
do {
42+
try {
43+
hello = future.get(10, TimeUnit.MILLISECONDS);
44+
} catch (TimeoutException ex) {
45+
System.err.println("Timeout! #" + ++counter);
46+
}
47+
} while (hello == null);
48+
System.out.println("Result: " + hello);
49+
50+
}
51+
52+
public static void testCompletableFuture() throws Exception {
53+
// CompletableFuture уже содержащий результат
54+
CompletableFuture<String> completed;
55+
completed = CompletableFuture.completedFuture("Просто значение");
56+
57+
// CompletableFuture, запускающий (run) новый поток с Runnable, поэтому он Void
58+
CompletableFuture<Void> voidCompletableFuture;
59+
voidCompletableFuture = CompletableFuture.runAsync(() -> {
60+
System.out.println("run " + Thread.currentThread().getName());
61+
});
62+
63+
// CompletableFuture, запускающий новый поток, результат которого возьмём у Supplier
64+
CompletableFuture<String> supplier;
65+
supplier = CompletableFuture.supplyAsync(() -> {
66+
System.out.println("supply " + Thread.currentThread().getName());
67+
return "Значение";
68+
});
69+
System.out.println(supplier.get());
3170
}
3271
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy