亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JavaScript使用yield模擬多線程的方法

 更新時(shí)間:2015年03月19日 11:50:24   作者:上大王  
這篇文章主要介紹了JavaScript使用yield模擬多線程的方法,實(shí)例分析了javascript多線程的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了JavaScript使用yield模擬多線程的方法。分享給大家供大家參考。具體分析如下:

在python和C#中都有yield方法,通過(guò)yield可以實(shí)現(xiàn)很多多線程才能實(shí)現(xiàn)的功能。
對(duì)javascript有版本要求:JavaScript 1.7

function Thread( name ) {
  for ( var i = 0; i < 5; i++ ) {
    Print(name+': '+i);
    yield;
  }
}
//// thread management
var threads = [];
// thread creation
threads.push( new Thread('foo') );
threads.push( new Thread('bar') );
// scheduler
while (threads.length) {
  var thread = threads.shift();
  try {
    thread.next();
    threads.push(thread);
  } catch(ex if ex instanceof StopIteration) {}
}

上面代碼輸入結(jié)果如下:

foo: 0
bar: 0
foo: 1
bar: 1
foo: 2
bar: 2
foo: 3
bar: 3
foo: 4
bar: 4

希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論