Skip to content

Commit 2c14852

Browse files
committed
new Constants
1 parent 605fa2b commit 2c14852

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
package net.codetojoy;
3+
4+
class Constants {
5+
static final int NUM_TASKS = 20;
6+
static final int NUM_TASKS_FOR_SUCCESS = 5;
7+
static final int MAX_DELAY_IN_SECONDS = 6;
8+
static final int PATHOGEN_THRESHOLD = 3; // 30% chance of failure
9+
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
// note: I no longer own this domain
33
package net.codetojoy;
44

5+
import static net.codetojoy.Constants.*;
6+
57
import java.util.*;
68
import java.util.stream.IntStream;
79
import java.util.concurrent.*;
810

9-
// javadoc here: https://download.java.net/java/early_access/jdk19/docs/api/jdk.incubator.concurrent/jdk/incubator/concurrent/package-summary.html
10-
1111
public class Runner {
1212
private final Random random = new Random(System.currentTimeMillis());
13-
private static final int MAX_DELAY_IN_SECONDS = 6;
1413

1514
String taskFoo(int i) {
1615
long delayInMillis = (random.nextInt(MAX_DELAY_IN_SECONDS) + 1) * 1000L;
@@ -23,11 +22,9 @@ String taskFoo(int i) {
2322
}
2423

2524
List<String> run() throws Exception {
26-
int numTasksForSuccess = 5;
27-
StructuredTaskScope.Joiner<String,List<String>> joiner = new CustomJoiner(numTasksForSuccess);
25+
StructuredTaskScope.Joiner<String,List<String>> joiner = new CustomJoiner(NUM_TASKS_FOR_SUCCESS);
2826
try (var scope = StructuredTaskScope.open(joiner)) {
29-
int numTasks = 50;
30-
IntStream.range(0, numTasks).forEach(i -> scope.fork(() -> taskFoo(i)));
27+
IntStream.range(0, NUM_TASKS).forEach(i -> scope.fork(() -> taskFoo(i)));
3128

3229
return scope.join();
3330
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
// note: I no longer own this domain
33
package net.codetojoy;
44

5+
import static net.codetojoy.Constants.*;
6+
57
import java.time.Duration;
68

79
class Worker {
8-
public static final int pathogenThreshold = 3;
910
public static final long THROW_EXCEPTION = -1L;
1011
public static final String LOG_PREFIX = "TRACER Worker: ";
1112

@@ -28,7 +29,7 @@ boolean isPathogenic() {
2829
int min = 1;
2930
int max = 10;
3031
int randomNum = min + (int)(Math.random() * ((max - min) + 1));
31-
return randomNum < pathogenThreshold;
32+
return randomNum < PATHOGEN_THRESHOLD;
3233
}
3334

3435
// see https://stackoverflow.com/questions/15160782

0 commit comments

Comments
 (0)