Skip to content

Instantly share code, notes, and snippets.

@pron
Forked from anonymous/quasarwhispers.java
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pron/33e0663dbb8bf63932d5 to your computer and use it in GitHub Desktop.
Save pron/33e0663dbb8bf63932d5 to your computer and use it in GitHub Desktop.
package foo;
import co.paralleluniverse.fibers.*;
import co.paralleluniverse.strands.*;
import co.paralleluniverse.strands.channels.*;
import static co.paralleluniverse.strands.channels.Channels.*;
public class quasarwhispers {
static void quasarWhispers(int n) throws Exception {
LongChannel leftmost = newLongChannel(0);
LongChannel left = leftmost, right = leftmost;
for (int i = 0; i < n; i++) {
right = newLongChannel(0);
LongChannel l = left, r = right; // must be "effectively final"
new Fiber<Void>(() -> { r.send(l.receive() + 1); }).start();
left = right;
}
new Fiber<Void>(() -> { leftmost.send(1); }).start();
System.out.println(right.receive());
}
static void main(String[] argv) throws Exception {
for (int i = 0; i < 20; i++)
quasarWhispers(1000000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment