Skip to content

Instantly share code, notes, and snippets.

@ebarlas
Created October 27, 2023 00:32
Show Gist options
  • Save ebarlas/86d3c494f8a249fa33c1e3a9609686e1 to your computer and use it in GitHub Desktop.
Save ebarlas/86d3c494f8a249fa33c1e3a9609686e1 to your computer and use it in GitHub Desktop.
Alternative implementation of rendezvous experiment by Adam Warski discussed here https://softwaremill.com/limits-of-looms-performance/
package com.barlasgarden;
import java.util.concurrent.atomic.AtomicInteger;
public class SimpleRendezvous {
public void test() throws Exception {
long start = System.currentTimeMillis();
final int max = 10_000_000;
AtomicInteger transfer = new AtomicInteger(-1);
Thread t1 = Thread.ofVirtual().start(() -> {
for (int i = 0; i <= max; i++) {
while (!transfer.compareAndSet(-1, i)) ;
}
});
Thread t2 = Thread.ofVirtual().start(() -> {
long acc = 0L;
int v;
for (int i = 0; i <= max; i++) {
while ((v = transfer.compareAndExchange(i, -1)) == -1) ;
acc += v;
}
assert acc == sumUpTo(max);
});
t1.join();
t2.join();
long end = System.currentTimeMillis();
System.out.println("Took: " + (end - start) + " ms");
}
private long sumUpTo(int max) {
return ((long) max * (max + 1)) / 2;
}
public static void main(String[] args) throws Exception {
for (int i = 0; i < 10; i++) {
new SimpleRendezvous().test();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment