Skip to content

Commit

Permalink
Update ConcurrentReaderWriter.java
Browse files Browse the repository at this point in the history
  • Loading branch information
bgprudhomme authored Aug 8, 2024
1 parent 9981865 commit f433ad3
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ConcurrentReaderWriter(Callable<Void> writer, Callable<Void> reader,

public void run() throws Exception {
for (int i = 0; i < numThreads / 2f; i++) {
new Thread(() -> { // writer thread
Thread.ofVirtual().start(() -> { // writer thread
try {
writer.call();
} catch (Exception e) {
Expand All @@ -43,9 +43,9 @@ public void run() throws Exception {
} finally {
writeCount.incrementAndGet();
}
}).start();
});

new Thread(() -> {
Thread.ofVirtual().start(() -> {
while (true) {
// Spin until we have something to read.
if (writeCount.get() > 0) {
Expand All @@ -62,7 +62,7 @@ public void run() throws Exception {
sleep(1);
}
}
}).start();
});
}

while (readCount.get() < numThreads / 2f ||
Expand Down

0 comments on commit f433ad3

Please sign in to comment.