Skip to content

Instantly share code, notes, and snippets.

@dogles
Created February 11, 2014 15:52
Show Gist options
  • Save dogles/8937583 to your computer and use it in GitHub Desktop.
Save dogles/8937583 to your computer and use it in GitHub Desktop.
com.dongxiguo.continuation.utils.ForkJoin.fork = function(threadIdentifiers,handler) {
if(Lambda.empty(threadIdentifiers)) throw "threadIdentifiers must not be empty!";
var counter = 1;
var quickJoinHandler = null;
var $it0 = $iterator(threadIdentifiers)();
while( $it0.hasNext() ) {
var id = $it0.next();
counter++;
var isJoined = [false];
handler(id,(function(isJoined) {
return function(joinHandler) {
if(isJoined[0]) throw "Cannot join twice in one thread!"; else {
isJoined[0] = true;
if(--counter == 0) joinHandler(); else quickJoinHandler = joinHandler;
}
};
})(isJoined));
}
if(--counter == 0) quickJoinHandler();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment