JavaScript和线程

JavaScript和线程,javascript,multithreading,Javascript,Multithreading,有什么方法可以在JavaScript中执行多线程处理吗?在原始JavaScript中,最好使用少量异步调用(xmlhttprequest),但这不是真正的线程处理,而且非常有限。向浏览器添加大量API,其中一些可用于线程支持。JavaScript中没有真正的线程。JavaScript作为一种可塑性很强的语言,确实允许您模拟其中的一些。这是我前几天遇到的一个问题。有关最新的支持信息,请参阅 以下是2009年左右的支持情况 你想在谷歌上搜索的词是 除此之外,目前还没有可用的解决方案,但是关于如何实

有什么方法可以在JavaScript中执行多线程处理吗?

在原始JavaScript中,最好使用少量异步调用(xmlhttprequest),但这不是真正的线程处理,而且非常有限。向浏览器添加大量API,其中一些可用于线程支持。

JavaScript中没有真正的线程。JavaScript作为一种可塑性很强的语言,确实允许您模拟其中的一些。这是我前几天遇到的一个问题。

有关最新的支持信息,请参阅

以下是2009年左右的支持情况


你想在谷歌上搜索的词是

除此之外,目前还没有可用的解决方案,但是关于如何实现这一点,有很多讨论,所以我想请关注这个问题,因为答案无疑将在未来发生变化

以下是齿轮的相关文档:

WHATWG对辅助线程有一个建议草案:

还有Mozilla的


更新:2009年6月,浏览器对JavaScript线程支持的当前状态

Firefox 3.5有网络工作者。如果您想看到web workers的一些演示,请执行以下操作:

  • (“试用”链接)
  • (邮件末尾的链接)
  • (第一环节)
Gears插件也可以安装在Firefox中

Safari 4WebKit nightlies具有工作线程:

Chrome内置了Gears,因此它可以执行线程,尽管它需要用户的确认提示(并且它对web workers使用不同的API,尽管它可以在安装Gears插件的任何浏览器中工作):

  • (这不是一个很好的例子,因为它运行得太快,无法在Chrome和Firefox中进行测试,尽管IE运行得太慢,足以看到它阻止了交互)

IE8IE9只能在安装Gears插件的情况下执行线程

Javascript中没有真正的多线程,但是可以使用
setTimeout()
和异步AJAX请求获得异步行为

您到底想实现什么?

您可以使用一个编译器,将您的代码转换为状态机,有效地让您模拟线程。它通过在语言中添加一个“屈服”操作符(表示为“->”)来实现这一点,允许您在单个线性代码块中编写异步代码。

现在支持它的新v8引擎(我认为)

具有HTML5“侧规范”,无需再使用setTimeout()、setInterval()等攻击javascript

HTML5&Friends引入了javascriptWebWorkers规范。它是一个用于异步和独立运行脚本的API


指向和的链接。

如果您不能或不想使用任何AJAX内容,请使用iframe或ten!;)您可以让进程在iframe中与母版页并行运行,而不必担心跨浏览器的可比问题或dot-net AJAX等的语法问题,并且您可以从iframe调用母版页的JavaScript(包括它导入的JavaScript)

例如,在父iframe中,加载iframe内容后调用父文档中的
egFunction()
(这是异步部分)


动态生成iFrame,以便主html代码不受它们的影响(如果需要)。

这里只是一种在Javascript中模拟多线程的方法

现在我将创建3个线程来计算数字加法,数字可以除以13,数字可以除以3直到1000000000。这三个函数不能像并发性所指的那样同时运行。但我将向您展示一个技巧,使这些函数同时递归运行:

这个密码是我的

身体部位

    <div class="div1">
    <input type="button" value="start/stop" onclick="_thread1.control ? _thread1.stop() : _thread1.start();" /><span>Counting summation of numbers till 10000000000</span> = <span id="1">0</span>
</div>
<div class="div2">
    <input type="button" value="start/stop" onclick="_thread2.control ? _thread2.stop() : _thread2.start();" /><span>Counting numbers can be divided with 13 till 10000000000</span> = <span id="2">0</span>
</div>
<div class="div3">
    <input type="button" value="start/stop" onclick="_thread3.control ? _thread3.stop() : _thread3.start();" /><span>Counting numbers can be divided with 3 till 10000000000</span> = <span id="3">0</span>
</div>

计数到1000000000的数字总和=0
计数数可以除以13,直到1000000000=0
计数数可以用3除,直到1000000000=0
Javascript部分

var _thread1 = {//This is my thread as object
    control: false,//this is my control that will be used for start stop
    value: 0, //stores my result
    current: 0, //stores current number
    func: function () {   //this is my func that will run
        if (this.control) {      // checking for control to run
            if (this.current < 10000000000) {
                this.value += this.current;   
                document.getElementById("1").innerHTML = this.value;
                this.current++;
            }
        }
        setTimeout(function () {  // And here is the trick! setTimeout is a king that will help us simulate threading in javascript
            _thread1.func();    //You cannot use this.func() just try to call with your object name
        }, 0);
    },
    start: function () {
        this.control = true;   //start function
    },
    stop: function () {
        this.control = false;    //stop function
    },
    init: function () {
        setTimeout(function () {
            _thread1.func();    // the first call of our thread
        }, 0)
    }
};
var _thread2 = {
    control: false,
    value: 0,
    current: 0,
    func: function () {
        if (this.control) {
            if (this.current % 13 == 0) {
                this.value++;
            }
            this.current++;
            document.getElementById("2").innerHTML = this.value;
        }
        setTimeout(function () {
            _thread2.func();
        }, 0);
    },
    start: function () {
        this.control = true;
    },
    stop: function () {
        this.control = false;
    },
    init: function () {
        setTimeout(function () {
            _thread2.func();
        }, 0)
    }
};
var _thread3 = {
    control: false,
    value: 0,
    current: 0,
    func: function () {
        if (this.control) {
            if (this.current % 3 == 0) {
                this.value++;
            }
            this.current++;
            document.getElementById("3").innerHTML = this.value;
        }
        setTimeout(function () {
            _thread3.func();
        }, 0);
    },
    start: function () {
        this.control = true;
    },
    stop: function () {
        this.control = false;
    },
    init: function () {
        setTimeout(function () {
            _thread3.func();
        }, 0)
    }
};

_thread1.init();
_thread2.init();
_thread3.init();
var\u thread1={//这是作为对象的我的线程
control:false,//这是我的控件,将用于启动和停止
值:0,//存储我的结果
当前:0,//存储当前编号
func:function(){//这是我将要运行的func
if(this.control){//正在检查控件是否运行
如果(当前<1000000000){
this.value+=this.current;
document.getElementById(“1”).innerHTML=this.value;
这个.current++;
}
}
setTimeout(函数(){//这里有个窍门!setTimeout是一个帮助我们在javascript中模拟线程的国王
_thread1.func();//您不能使用此.func(),请尝试使用对象名调用
}, 0);
},
开始:函数(){
this.control=true;//启动函数
},
停止:函数(){
this.control=false;//停止函数
},
init:函数(){
setTimeout(函数(){
_thread1.func();//线程的第一次调用
}, 0)
}
};
变量_thread2={
控制:错误,
值:0,
电流:0,
func:function(){
如果(此控件){
如果(当前%13==0){
这个.value++;
}
这个.current++;
document.getElementById(“2”).innerHTML=this.value;
}
setTimeout(函数(){
_thread2.func();
}, 0);
},
开始:函数(){
这个控制=真;
},
停止:函数(){
这个控制=假;
},
init:函数(){
setTimeout(函数(){
_thread2.func();
}, 0)
}
};
变量_thread3={
控制:错误,
值:0,
电流:0,
func:function(){
如果(这句话)
var _thread1 = {//This is my thread as object
    control: false,//this is my control that will be used for start stop
    value: 0, //stores my result
    current: 0, //stores current number
    func: function () {   //this is my func that will run
        if (this.control) {      // checking for control to run
            if (this.current < 10000000000) {
                this.value += this.current;   
                document.getElementById("1").innerHTML = this.value;
                this.current++;
            }
        }
        setTimeout(function () {  // And here is the trick! setTimeout is a king that will help us simulate threading in javascript
            _thread1.func();    //You cannot use this.func() just try to call with your object name
        }, 0);
    },
    start: function () {
        this.control = true;   //start function
    },
    stop: function () {
        this.control = false;    //stop function
    },
    init: function () {
        setTimeout(function () {
            _thread1.func();    // the first call of our thread
        }, 0)
    }
};
var _thread2 = {
    control: false,
    value: 0,
    current: 0,
    func: function () {
        if (this.control) {
            if (this.current % 13 == 0) {
                this.value++;
            }
            this.current++;
            document.getElementById("2").innerHTML = this.value;
        }
        setTimeout(function () {
            _thread2.func();
        }, 0);
    },
    start: function () {
        this.control = true;
    },
    stop: function () {
        this.control = false;
    },
    init: function () {
        setTimeout(function () {
            _thread2.func();
        }, 0)
    }
};
var _thread3 = {
    control: false,
    value: 0,
    current: 0,
    func: function () {
        if (this.control) {
            if (this.current % 3 == 0) {
                this.value++;
            }
            this.current++;
            document.getElementById("3").innerHTML = this.value;
        }
        setTimeout(function () {
            _thread3.func();
        }, 0);
    },
    start: function () {
        this.control = true;
    },
    stop: function () {
        this.control = false;
    },
    init: function () {
        setTimeout(function () {
            _thread3.func();
        }, 0)
    }
};

_thread1.init();
_thread2.init();
_thread3.init();
function blocking (exampleArgument) {
    // block thread
}

// turn blocking pure function into a worker task
const blockingAsync = task.wrap(blocking);

// run task on a autoscaling worker pool
blockingAsync('exampleArgumentValue').then(result => {
    // do something with result
});