Skip to content

Instantly share code, notes, and snippets.

@jmlacroix
Created April 14, 2010 16:55
Show Gist options
  • Save jmlacroix/366048 to your computer and use it in GitHub Desktop.
Save jmlacroix/366048 to your computer and use it in GitHub Desktop.
Threaded javascript proof of concept. Found in my 2006 archives. Firefox only.
<html><body>
<div id="o"></div>
<script>
// Thread scheduling.
var Scheduler = {
threads : new Queue(),
run : function(context) {
Scheduler.threads.insert(context);
Scheduler.schedule();
},
schedule : function() {
var context = Scheduler.threads.remove();
if (context instanceof Context) {
context = context.exec();
Scheduler.threads.insert(context);
// var rand = new Date().getTime().toString();
// rand = parseInt(rand[rand.length-2] + rand[rand.length-1]);
setTimeout(Scheduler.schedule, 1);
}
}
};
// Thread queue.
function Queue() {
// Queue variables.
var queue = new Array();
var length = 0;
var empty = true;
// Manage queue length.
function setLength(value) {
length += value;
empty = length == 0;
return !empty;
}
// Insert an element at the end of the queue.
this.insert = function(element) {
setLength(+1);
queue.push(element);
}
// Remove the head of the queue.
this.remove = function() {
if (empty) {
return null;
} else {
var element = queue.shift();
setLength(-1);
return element;
}
}
// Returns the first element without removing it.
this.peek = function() {
if (!empty) {
return queue[0];
} else {
return null;
}
}
// Return the queue as a String.
this.toString = function() {
return queue.toString();
}
// Initialize the queue to the received elements.
for (var i=0; i<arguments.length; i++) {
this.insert(arguments[i]);
}
}
String.prototype.repeat = function(times) {
for (var i=0, result = ""; i<times; i++) {
result += this;
}
return result;
}
function Context(func) {
this.exec = func;
}
function Thread(func) {
var func = func;
// Transform the function in a threadable one.
this.threadify = function(func) {
var code = func.valueOf().toString();
code = code.slice(code.indexOf('\n') + 1, code.lastIndexOf('\n'));
var length = code.split('\n').length;
code = code.replace(/{\n/ig, "{");
code = code.replace(/\n.*}/ig, "}");
code = code.replace(/\n/ig, "\nreturn new Context(function() {\n") + "\n});".repeat(length-3);
return new Function(code);
}
// Start the execution of the threaded function.
this.start = function() {
Scheduler.run(new Context(func));
}
// Stop the execution of the threaded function.
this.stop = function() {
}
// Pause the execution of the threaded function.
this.pause = function() {
}
// Transform the function in a threadable one.
func = this.threadify(func);
return this;
}
function writing() {
d("1");
d("2");
d("3");
d("4");
d("5");
for (var i=0; i<5; i++) {
d("5." + i);
}
d("6");
d("7");
d("8");
d("9");
}
var t1 = new Thread(writing);
var t2 = new Thread(writing);
var t3 = new Thread(writing);
t1.start();
t2.start();
t3.start();
function d(txt) {
document.getElementById('o').innerHTML += txt + "<br>";
}
</script>
@jmlacroix
Copy link
Author

Output:

1
2
1
3
1
2
4
2
3
5
3
4
5.0
5.1
5.2
5.3
5.4
4
5
6
5
5.0
5.1
5.2
5.3
5.4
7
5.0
5.1
5.2
5.3
5.4
6
8
6
7
9
7
8
8
9
9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment