-
-
Save pron/76147aa66f076c783da8 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 SuspendableRunnable whisper(LongChannel left, LongChannel right) { | |
return () -> { right.send(left.receive() + 1); } | |
} | |
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); | |
new Fiber<Void>(whisper(left, right)).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