Skip to content

Commit f1554ea

Browse files
authored
Project Loom examples and updated presentation (#35)
1 parent d018dfb commit f1554ea

4 files changed

Lines changed: 70 additions & 12 deletions

File tree

README.adoc

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

65
++++
76
<p align="center">
@@ -32,9 +31,9 @@ The project also provides, as documentation, a presentation about asynchronous a
3231
|Name |Version |Download |Updated at | Lang
3332

3433
|Programación asíncrona y reactiva en Java
35-
|0.0.14-SNAPSHOT
36-
| https://drive.google.com/file/d/10_VuEaIZ5M0RTJU-D_MO5yB_nNOvFqV3/view?usp=sharing[pptx, window=_blank] or https://drive.google.com/file/d/1NNjiHppqJEZLECzwcN-WSSWs_2bNrsr5/view?usp=sharing[pdf, window=_blank]
37-
|2020-12-04
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
3837
|🇪🇸
3938

4039
|Asynchronous and reactive programming in Java

examples/06-project-loom/loom/src/main/java/es/codeurjc/arpj/HelloLoom.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@ public static void main(String[] args) throws InterruptedException {
66

77
System.out.println("Hello @ " + Thread.currentThread().getName());
88

9-
Thread myVt = Thread.builder()
10-
.virtual()
11-
.name("my-virtual-thread")
12-
.task(() -> System.out.println("Hello Loom! @ " + Thread.currentThread().getName()))
13-
.build();
14-
myVt.start();
9+
final Thread myVt = Thread.startVirtualThread(
10+
() -> System.out.println("Hello Loom! @ "
11+
+ Thread.currentThread().getName()));
12+
myVt.join();
1513

16-
Thread.sleep(1000);
1714
System.out.println("Goodbye @ " + Thread.currentThread().getName());
1815
}
1916
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package es.codeurjc.arpj;
2+
3+
public class LoomThreadBuilder {
4+
5+
public static void main(String[] args) throws InterruptedException {
6+
7+
System.out.println("Hello @ " + Thread.currentThread().getName());
8+
9+
Thread myVt = Thread.builder()
10+
.virtual()
11+
.name("my-virtual-thread")
12+
.task(() -> System.out.println("Hello Loom! @ "
13+
+ Thread.currentThread().getName()))
14+
.build();
15+
myVt.start();
16+
17+
Thread.sleep(1000);
18+
System.out.println("Goodbye @ " + Thread.currentThread().getName());
19+
}
20+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package es.codeurjc.arpj;
2+
3+
import java.security.SecureRandom;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.ThreadFactory;
7+
8+
public class ThreeMillionTasks {
9+
10+
private static final SecureRandom RANDOM = new SecureRandom();
11+
12+
public static void main(String[] args) {
13+
14+
ThreadFactory factory = Thread.builder().virtual()
15+
.name("my-vt-factory").factory();
16+
17+
final ExecutorService executorService =
18+
Executors.newThreadExecutor(factory);
19+
20+
for (int i = 0; i < 3_000_000; i++) {
21+
int index = i;
22+
executorService.submit(() -> {
23+
try {
24+
task(index);
25+
} catch (final InterruptedException e) {
26+
e.printStackTrace();
27+
}
28+
});
29+
}
30+
executorService.close();
31+
}
32+
33+
34+
private static void task(final int taskNumber)
35+
throws InterruptedException {
36+
37+
System.out.println("Sleeping task " + taskNumber + "... !!! @ "
38+
+ Thread.currentThread().getName());
39+
40+
Thread.sleep(RANDOM.nextInt(10) * 500L);
41+
}
42+
}

0 commit comments

Comments
 (0)