Skip to content

Commit

Permalink
Adding new loom examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jabrena committed May 29, 2024
1 parent 84c3f97 commit afc6bec
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 176 deletions.
81 changes: 81 additions & 0 deletions training/src/main/java/info/jab/fp/async/LoomExamples.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package info.jab.fp.async;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.StructuredTaskScope;

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

import java.util.concurrent.StructuredTaskScope.Subtask.State;
import java.util.concurrent.TimeoutException;

public class LoomExamples {

private static final Logger logger = LoggerFactory.getLogger(LoomExamples.class);
Expand Down Expand Up @@ -77,4 +83,79 @@ public Integer usingVThread4() {
return result;
}
}

public State usingVThread5() {
try (var scope = new StructuredTaskScope<Integer>()) {
var task = scope.fork(() -> {
Thread.sleep(1_000);
return 42;
});
logger.info("{}",task.state()); // UNAVAILABLE
try {
scope.join();
} catch (InterruptedException e) { }
logger.info("{}",task.state()); // SUCCESS
return task.state();
}
}

public Integer usingVThread6() {
var result = 0;
try (var scope = new StructuredTaskScope.ShutdownOnSuccess<Integer>()) {
scope.fork(() -> {
Thread.sleep(1_000);
return 1;
});
scope.fork(() -> {
Thread.sleep(42);
return 2;
});
try {
result = scope.join().result();
} catch (InterruptedException | ExecutionException e) { }
}
return result;
}

public Integer usingVThread7() {
var result = 0;
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
var task1 = scope.fork(() -> {
Thread.sleep(1_000);
return 1;
});
var task2 = scope.<String>fork(() -> {
Thread.sleep(42);
return "2";
});
try {
scope.join().throwIfFailed();
} catch (InterruptedException | ExecutionException ex) {}
System.out.println(task1.get() + task2.get());
}
return result;
}

public Integer usingVThread8() {
var result = 0;

try (var scope = new StructuredTaskScope<>()) {
var task1 = scope.fork(() -> {
Thread.sleep(1_000); // throws InterruptedException
return 1;
});
var task2 = scope.fork(() -> {
Thread.sleep(5_000); // throws InterruptedException
return 2;
});
try {
scope.joinUntil(Instant.now().plus(Duration.ofMillis(100)));
} catch (TimeoutException | InterruptedException e) {
//scope.shutdown();
}
System.out.println(task1.state()); // UNAVAILABLE
System.out.println(task2.state()); // UNAVAILABLE
}
return result;
}
}

This file was deleted.

This file was deleted.

This file was deleted.

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

This file was deleted.

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

This file was deleted.

35 changes: 35 additions & 0 deletions training/src/test/java/info/jab/fp/async/LoomExamplesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@

package info.jab.fp.async;

import java.util.concurrent.StructuredTaskScope.Subtask.State;

import static org.assertj.core.api.BDDAssertions.then;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import info.jab.utils.TestLoggerExtension;


@ExtendWith(TestLoggerExtension.class)
public class LoomExamplesTest {
Expand Down Expand Up @@ -53,4 +56,36 @@ public void usingVThread4() {
//Then
then(result).isEqualTo(3);
}

@Test
public void usingVThread5() {
var result = loomExamples.usingVThread5();

//Then
then(result).isEqualTo(State.SUCCESS);
}

@Test
public void usingVThread6() {
var result = loomExamples.usingVThread6();

//Then
then(result).isEqualTo(2);
}

@Test
public void usingVThread7() {
var result = loomExamples.usingVThread7();

//Then
then(result).isEqualTo(0);
}

@Test
public void usingVThread8() {
var result = loomExamples.usingVThread8();

//Then
then(result).isEqualTo(0);
}
}

0 comments on commit afc6bec

Please sign in to comment.