-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
140 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
training/src/main/java/info/jab/fp/async/LoomExamples.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
135
training/src/main/java/info/jab/fp/async/loom/_14_http_server.java
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
training/src/main/java/info/jab/fp/async/loom/_1_starting_thread.java
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
training/src/main/java/info/jab/fp/async/loom/_2_thread_builder.java
This file was deleted.
Oops, something went wrong.
32 changes: 0 additions & 32 deletions
32
training/src/main/java/info/jab/fp/async/loom/_3_how_many_platform_thread.java
This file was deleted.
Oops, something went wrong.
32 changes: 0 additions & 32 deletions
32
training/src/main/java/info/jab/fp/async/loom/_4_how_many_virtual_thread.java
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
training/src/main/java/info/jab/fp/async/loom/_6_thread_local.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.