Skip to content

Commit 605fa2b

Browse files
committed
refactor
1 parent d8310b2 commit 605fa2b

File tree

2 files changed

+13
-46
lines changed

2 files changed

+13
-46
lines changed

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

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
public class CustomJoiner<T> implements Joiner<T,List<T>> {
1717
private static final String LOG_PREFIX = "TRACER CustomJoiner ";
1818

19-
private AtomicInteger successCounter = new AtomicInteger(0);
20-
private AtomicBoolean hasReachedThreshold = new AtomicBoolean(false);
21-
private int numTasksForSuccess = 0;
22-
private List<T> results = new CopyOnWriteArrayList<>();
19+
private final AtomicInteger successCounter = new AtomicInteger(0);
20+
private final AtomicBoolean hasReachedThreshold = new AtomicBoolean(false);
21+
private final int numTasksForSuccess;
22+
private final List<T> results = new CopyOnWriteArrayList<>();
2323

2424
// sanity check:
2525
private AtomicInteger failCounter = new AtomicInteger(0);
@@ -57,49 +57,14 @@ public boolean onComplete(StructuredTaskScope.Subtask<? extends T> subtask) {
5757

5858
@Override
5959
public List<T> result() {
60+
String stats = " num ok: " + successCounter.get() + " num failed: " + failCounter.get();
6061
if (! hasReachedThreshold.get()) {
62+
System.out.println(LOG_PREFIX + " failed. stats: " + stats);
6163
throw new IllegalStateException("success threshold not met");
6264
}
63-
System.out.println(LOG_PREFIX + " success! num ok: " + numTasksForSuccess +
64-
" num failed: " + failCounter.get());
6565

66-
return results;
67-
}
68-
69-
/*
70-
*
71-
@Override
72-
protected void handleComplete(StructuredTaskScope.Subtask<? extends T> subtask) {
73-
try {
74-
var state = subtask.state();
75-
if (state == StructuredTaskScope.Subtask.State.SUCCESS) {
76-
int numSuccess = successCounter.incrementAndGet();
77-
if (numSuccess <= numTasksForSuccess) {
78-
results.add(subtask.get());
79-
}
66+
System.out.println(LOG_PREFIX + " success. stats: " + stats);
8067

81-
if (numSuccess == numTasksForSuccess) {
82-
hasReachedThreshold.getAndSet(true);
83-
System.out.println(LOG_PREFIX + " success threshold reached...");
84-
shutdown();
85-
}
86-
} else if (state == StructuredTaskScope.Subtask.State.FAILED) {
87-
failCounter.incrementAndGet();
88-
}
89-
} catch (Exception ex) {
90-
System.err.println(LOG_PREFIX + " ERROR caught ex: " + ex.getMessage());
91-
}
92-
}
93-
94-
// TODO: deep copy?
95-
public List<T> results() {
96-
if (! hasReachedThreshold.get()) {
97-
throw new IllegalStateException("success threshold not met");
98-
}
99-
System.out.println(LOG_PREFIX + " success! num ok: " + numTasksForSuccess +
100-
" num failed: " + failCounter.get());
101-
102-
return new ArrayList<T>(results);
68+
return results;
10369
}
104-
*/
10570
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.time.Duration;
66

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

@@ -24,9 +25,10 @@ void logInfo(int index, String name) {
2425
}
2526

2627
boolean isPathogenic() {
27-
long now = System.currentTimeMillis();
28-
long remainder = now % 10;
29-
return remainder <= 3;
28+
int min = 1;
29+
int max = 10;
30+
int randomNum = min + (int)(Math.random() * ((max - min) + 1));
31+
return randomNum < pathogenThreshold;
3032
}
3133

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

0 commit comments

Comments
 (0)