[JavaScript] JavaScript使用yield模拟多线程的程序 →→→→→进入此内容的聊天室

来自 , 2019-06-03, 写在 JavaScript, 查看 130 次.
URL http://www.code666.cn/view/b1790a55
  1. //// thread definition,http://www.sharejs.com
  2. function Thread( name ) {
  3.  
  4.     for ( var i = 0; i < 5; i++ ) {
  5.  
  6.         Print(name+': '+i);
  7.         yield;
  8.     }
  9. }
  10.  
  11. //// thread management
  12. var threads = [];
  13.  
  14. // thread creation
  15. threads.push( new Thread('foo') );
  16. threads.push( new Thread('bar') );
  17.  
  18. // scheduler
  19. while (threads.length) {
  20.  
  21.     var thread = threads.shift();
  22.     try {
  23.         thread.next();
  24.         threads.push(thread);
  25.     } catch(ex if ex instanceof StopIteration) {}
  26. }
  27.  
  28.  
  29. //javascript/8767

回复 "JavaScript使用yield模拟多线程的程序"

这儿你可以回复上面这条便签

captcha