Skip to content

Commit 769c2d9

Browse files
committed
egg8 jdk 25
1 parent 12eafec commit 769c2d9

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

egg_8_sc_custom_invoke_some/compile.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ TARGET_DIR=$ROOT_DIR/my_build/main
1010

1111
mkdir -p $TARGET_DIR
1212

13-
javac --release 21 --enable-preview \
13+
javac --release 25 --enable-preview \
1414
-cp $CLASSPATH \
1515
-d $TARGET_DIR `find $SRC_DIR -name "*.java"`
1616

egg_8_sc_custom_invoke_some/run.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ CLASSPATH=$CLASSPATH:$TARGET_DIR
99

1010
java --enable-preview \
1111
-cp $CLASSPATH \
12-
--add-modules jdk.incubator.concurrent \
1312
net.codetojoy.Runner
1413

1514
echo "run complete"

egg_8_sc_custom_invoke_some/src/main/java/net/codetojoy/CustomStructuredTaskScope.java renamed to egg_8_sc_custom_invoke_some/src/main/java/net/codetojoy/CustomJoiner.java

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
import java.util.*;
66
import java.util.concurrent.*;
77
import java.util.concurrent.atomic.*;
8+
import java.util.concurrent.StructuredTaskScope.Joiner;
89

910
// NOTE: this is NOT production-ready, just an experiment
1011
//
11-
// this is similar in spirit to StructuredTaskScope.ShutdownOnSuccess<T>, but for N tasks
12+
// this is similar in spirit to ShutdownOnSuccess<T>, but for N tasks
1213
// i.e. given M tasks, success is defined when N of them complete successfully
1314
// to wit: `invokeSome(n)`
1415

15-
public class CustomStructuredTaskScope<T> extends StructuredTaskScope<T> {
16-
private static final String LOG_PREFIX = "TRACER CustomStructuredTaskScope ";
16+
public class CustomJoiner<T> implements Joiner<T,List<T>> {
17+
private static final String LOG_PREFIX = "TRACER CustomJoiner ";
1718

1819
private AtomicInteger successCounter = new AtomicInteger(0);
1920
private AtomicBoolean hasReachedThreshold = new AtomicBoolean(false);
@@ -23,10 +24,50 @@ public class CustomStructuredTaskScope<T> extends StructuredTaskScope<T> {
2324
// sanity check:
2425
private AtomicInteger failCounter = new AtomicInteger(0);
2526

26-
public CustomStructuredTaskScope(int numTasksForSuccess) {
27+
public CustomJoiner(int numTasksForSuccess) {
2728
this.numTasksForSuccess = numTasksForSuccess;
2829
}
2930

31+
@Override
32+
public boolean onComplete(StructuredTaskScope.Subtask<? extends T> subtask) {
33+
boolean result = false;
34+
35+
try {
36+
var state = subtask.state();
37+
if (state == StructuredTaskScope.Subtask.State.SUCCESS) {
38+
int numSuccess = successCounter.incrementAndGet();
39+
if (numSuccess <= numTasksForSuccess) {
40+
results.add(subtask.get());
41+
}
42+
43+
if (numSuccess == numTasksForSuccess) {
44+
hasReachedThreshold.getAndSet(true);
45+
System.out.println(LOG_PREFIX + " success threshold reached...");
46+
result = true;
47+
}
48+
} else if (state == StructuredTaskScope.Subtask.State.FAILED) {
49+
failCounter.incrementAndGet();
50+
}
51+
} catch (Exception ex) {
52+
System.err.println(LOG_PREFIX + " ERROR caught ex: " + ex.getMessage());
53+
}
54+
55+
return result;
56+
}
57+
58+
@Override
59+
public List<T> result() {
60+
if (! hasReachedThreshold.get()) {
61+
throw new IllegalStateException("success threshold not met");
62+
}
63+
System.out.println(LOG_PREFIX + " success! num ok: " + numTasksForSuccess +
64+
" num failed: " + failCounter.get());
65+
66+
return results;
67+
}
68+
69+
/*
70+
*
3071
@Override
3172
protected void handleComplete(StructuredTaskScope.Subtask<? extends T> subtask) {
3273
try {
@@ -60,4 +101,5 @@ public List<T> results() {
60101
61102
return new ArrayList<T>(results);
62103
}
104+
*/
63105
}

egg_8_sc_custom_invoke_some/src/main/java/net/codetojoy/Runner.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.*;
66
import java.util.stream.IntStream;
7+
import java.util.concurrent.*;
78

89
// javadoc here: https://download.java.net/java/early_access/jdk19/docs/api/jdk.incubator.concurrent/jdk/incubator/concurrent/package-summary.html
910

@@ -23,13 +24,12 @@ String taskFoo(int i) {
2324

2425
List<String> run() throws Exception {
2526
int numTasksForSuccess = 5;
26-
try (var scope = new CustomStructuredTaskScope<String>(numTasksForSuccess)) {
27+
StructuredTaskScope.Joiner<String,List<String>> joiner = new CustomJoiner(numTasksForSuccess);
28+
try (var scope = StructuredTaskScope.open(joiner)) {
2729
int numTasks = 50;
2830
IntStream.range(0, numTasks).forEach(i -> scope.fork(() -> taskFoo(i)));
2931

30-
scope.join();
31-
32-
return scope.results();
32+
return scope.join();
3333
}
3434
}
3535

0 commit comments

Comments
 (0)