-
-
Save pron/33e0663dbb8bf63932d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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