Skip to content

Commit

Permalink
Improving Loom examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jabrena committed May 29, 2024
1 parent db592f0 commit 84c3f97
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 340 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ A repository to review the main concepts about Functional Programming with Java.
sdk env install
./mvnw clean test -DexcludedGroups=performance,endtoend
./mvnw clean test -DexcludedGroups=performance,endtoend -pl training
./mvnw clean test -DexcludedGroups=performance,endtoend -Dtest=FunctionalInterfacesExamplesTest -pl training
./mvnw clean test -DexcludedGroups=performance,endtoend -Dtest=LoomExamplesTest -pl training
./mvnw clean test -Dgroups=performance
./mvnw clean test -Dgroups=endtoend

Expand All @@ -27,7 +27,7 @@ sdk env install
## Functional programming features in Java

- [x] Lambda Expressions (Functional interfaces, Functions, Supplier, Consumer & Predicates)
- [x] Optionals
- [x] Optional
- [x] Stream API
- [x] CompletableFuture & Structural Concurrency
- [ ] Immutable Lists
Expand All @@ -39,9 +39,9 @@ sdk env install

| Java Version | Feature | Date | Release notes |
|--------------|-------------------------------------------------------------------------------------|-----------|------------------------------------------------------------------------|
| Java 8 | - Lambda Expressions - Optionals - Stream API - CompletableFuture | 18/3/2014 | https://www.oracle.com/java/technologies/javase/8-whats-new.html |
| Java 8 | - Lambda Expressions - Optional - Stream API - CompletableFuture | 18/3/2014 | https://www.oracle.com/java/technologies/javase/8-whats-new.html |
| Java 9 | - CompletableFuture updates | 21/9/2017 | https://www.oracle.com/java/technologies/javase/9-all-relnotes.html |
| Java 10 | - Optionals updates - Immutable Lists | 20/3/2018 | https://www.oracle.com/java/technologies/javase/10-relnote-issues.html |
| Java 10 | - Optional updates - Immutable Lists | 20/3/2018 | https://www.oracle.com/java/technologies/javase/10-relnote-issues.html |
| Java 11 | - Not Predicate operator - Local-Variable Syntax for Lambda | 25/9/2018 | https://www.oracle.com/java/technologies/javase/11all-relnotes.html |
| Java 12 | - Teeing Collector - Pattern Matching | 19/3/2019 | https://www.oracle.com/java/technologies/javase/12-relnote-issues.html |
| Java 13 | - Switch Expressions enhancements | 17/9/2019 | https://www.oracle.com/java/technologies/javase/13-relnote-issues.html |
Expand Down
80 changes: 80 additions & 0 deletions training/src/main/java/info/jab/fp/async/LoomExamples.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package info.jab.fp.async;

import java.util.concurrent.StructuredTaskScope;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoomExamples {

private static final Logger logger = LoggerFactory.getLogger(LoomExamples.class);

public void usingVThread1() {
try {
// virtual thread
var vthread = Thread.ofVirtual()
.name("virtual-", 0)
.start(() -> {
logger.info("virtual " + Thread.currentThread());
});

vthread.join();
} catch (InterruptedException e) {

}
}

private static final ThreadLocal<String> USER = new ThreadLocal<>();

public String usingVThread2() {
USER.set("White");
try {
var vthread = Thread.ofVirtual().start(() -> {
USER.set("Black");
logger.info("Hello " + USER.get());
USER.remove();
logger.info("Hello " + USER.get());
});
vthread.join();
} catch (InterruptedException e) { }
return USER.get();
}

//TODO Review ScopedValue in detail
private static final ScopedValue<String> USER2 = ScopedValue.newInstance();

public String usingVThread3() {

ScopedValue.runWhere(USER2, "Black", () -> {});
try {
var vthread = Thread.ofVirtual()
.start(() -> {
ScopedValue.runWhere(USER2, "White", () -> {
logger.info("Hello " + USER2.get());
});
});
vthread.join();
} catch (InterruptedException e) { }
return USER2.get();
}

public Integer usingVThread4() {
try (var scope = new StructuredTaskScope<Integer>()) {
var task1 = scope.fork(() -> {
Thread.sleep(1_000);
return 1;
});
var task2 = scope.fork(() -> {
Thread.sleep(1_000);
return 2;
});

try {
scope.join();
} catch (InterruptedException e) { }

var result = task1.get() + task2.get();
return result;
}
}
}
135 changes: 0 additions & 135 deletions training/src/main/java/info/jab/fp/async/loom/_14_http_server.java

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

23 changes: 0 additions & 23 deletions training/src/main/java/info/jab/fp/async/loom/_6_thread_local.java

This file was deleted.

Loading

0 comments on commit 84c3f97

Please sign in to comment.