Skip to content

Commit 48856b0

Browse files
authored
Add some examples about Project Loom. Updated presentation (1.0.0-M1) (#36)
* Add some examples about Project Loom. Updated presentation (1.0.0-M1) * Restore table-of-contents
1 parent f1554ea commit 48856b0

3 files changed

Lines changed: 121 additions & 4 deletions

File tree

README.adoc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
= Asynchronous & reactive programming in Java
22
Marcos de la Calle Samaniego, @marcosDLCS <marcos.dlcs@gmail.com>
3-
v0.0.16, 2020-12-06
3+
v1.0.0, 2020-12-07
4+
:toc:
45

56
++++
67
<p align="center">
@@ -31,9 +32,9 @@ The project also provides, as documentation, a presentation about asynchronous a
3132
|Name |Version |Download |Updated at | Lang
3233

3334
|Programación asíncrona y reactiva en Java
34-
|0.0.15-SNAPSHOT
35-
| https://drive.google.com/file/d/1tKLL--rBfonlss4V3wWtxiok1eGiU9le/view?usp=sharing[pptx, window=_blank] or https://drive.google.com/file/d/1CA6qHuB_3BLbD9eX2ibbAhkKR159CYAv/view?usp=sharing[pdf, window=_blank]
36-
|2020-12-06
35+
|1.0.0-M1
36+
| https://drive.google.com/file/d/1fUeRK2f4S3cV50-atfIrqswEHoeu4HGc/view?usp=sharing[pptx, window=_blank] or https://drive.google.com/file/d/1tb4gk7pkC-hMosvAjXBN9YEG0VsBLOi_/view?usp=sharing[pdf, window=_blank]
37+
|2020-12-07
3738
|🇪🇸
3839

3940
|Asynchronous and reactive programming in Java
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package es.codeurjc.arpj;
2+
3+
import java.time.Instant;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.atomic.AtomicInteger;
6+
7+
public class CancellationDeadline {
8+
9+
public static void main(String[] args) {
10+
11+
final AtomicInteger success = new AtomicInteger();
12+
final AtomicInteger fail = new AtomicInteger();
13+
14+
try (final var executorService = Executors.newVirtualThreadExecutor()
15+
.withDeadline(Instant.now().plusSeconds(1))) {
16+
17+
for (int i = 0; i < 500; i++) {
18+
19+
final var index = i;
20+
executorService.submit(() -> {
21+
try {
22+
print(index);
23+
success.incrementAndGet();
24+
} catch (final InterruptedException e) {
25+
System.out.println("Interrupted exception !!!");
26+
fail.incrementAndGet();
27+
}
28+
});
29+
}
30+
31+
System.out.println("\nAll tasks have been submitted!!!\n");
32+
}
33+
34+
System.out.println("\nEverything has finished!!!\n");
35+
System.out.println("Success: " + success.get());
36+
System.out.println("Fail : " + fail.get());
37+
}
38+
39+
private static void print(final int taskNumber)
40+
throws InterruptedException {
41+
42+
if (taskNumber % 2 == 0) {
43+
Thread.sleep(965);
44+
}
45+
46+
System.out.println("Normal task [my-task] - " + taskNumber + "... !!! @ "
47+
+ Thread.currentThread().getName());
48+
}
49+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package es.codeurjc.arpj;
2+
3+
import java.util.concurrent.Executors;
4+
import java.util.concurrent.ThreadFactory;
5+
6+
public class StructuredConcurrency {
7+
8+
public static void main(String[] args) {
9+
10+
ThreadFactory factory = Thread.builder().virtual()
11+
.name("my-vt-factory-1").factory();
12+
13+
try (final var executorService = Executors.newThreadExecutor(factory)) {
14+
15+
for (int i = 0; i < 500; i++) {
16+
17+
final var index = i;
18+
19+
if (index % 2 == 0) {
20+
executorService.submit(() -> taskTwo(index));
21+
} else {
22+
executorService.submit(() -> taskOne(index));
23+
}
24+
}
25+
26+
System.out.println("\nAll tasks have been submitted!!!\n");
27+
}
28+
29+
System.out.println("\nEverything has finished!!!\n");
30+
}
31+
32+
private static void taskOne(final int taskNumber) {
33+
34+
ThreadFactory factory = Thread.builder().virtual()
35+
.name("my-factory-task-one").factory();
36+
37+
try (final var executorService = Executors.newThreadExecutor(factory)) {
38+
39+
executorService.submit(() -> print("t-one", taskNumber));
40+
}
41+
}
42+
43+
private static void taskTwo(final int taskNumber) {
44+
45+
ThreadFactory factory = Thread.builder().virtual()
46+
.name("my-factory-task-two").factory();
47+
48+
try (final var executorService = Executors.newThreadExecutor(factory)) {
49+
50+
executorService.submit(() -> print("t-two", taskNumber));
51+
}
52+
}
53+
54+
private static void print(final String task, final int taskNumber) {
55+
56+
if (taskNumber % 2 == 0) {
57+
try {
58+
Thread.sleep(50);
59+
} catch (InterruptedException e) {
60+
e.printStackTrace();
61+
}
62+
}
63+
64+
System.out.println("Normal task [" + task + "] - " + taskNumber + "... !!! @ "
65+
+ Thread.currentThread().getName());
66+
}
67+
}

0 commit comments

Comments
 (0)