`
q445862108
  • 浏览: 79373 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
社区版块
存档分类
最新评论

js 线程

    博客分类:
  • js
 
阅读更多
Concurrent.Thread API中文文档
API原文(日文):http://jsthread.sourceforge.net/cgi-bin/wiki/wiki.cgi?page=API_Specification
Goal(目标)

//运行函数中的线程var ui_t = Concurrent.Thread.create(function(){ var i = 0; while ( 1 ) { document.body.innerHTML = "Now calculating..."                            + ['|', '/', '-', '\'][i]; i = (i+1) % 4; Concurrent.Thread.sleep(1000); }});

//時間がかかりそうな計算をする(別スレッド化された)関数var fibonacci = Concurrent.Thread.compile(function(x){ function fib ( n ) { return n <= 1  ?  1  :  fib(n-2) + fib(n-1); } var arr = new Array(); for ( var i=0;  i < x;  i++ ) arr.push(fib(i)); return arr;});

//主线程.var main_t = Concurrent.Thread.create(function(){ var calc_t = fibonacci.async(null, [1000]);  //从1000开始运行线程 var result = calc_t.join();                  //等待运行完的结果 ui_t.kill();                                 //バーを回していたスレッドを停止 focument.body.innerHTML = "Now calculating...finised.<br>\n"; for ( var i=0;  i < result.length;  i++ ) { document.body.innerHTML += i + ": " + result[i] + "<br>\n"; }});

主要的目标是下面3点

    * 提供对象的线程库
    * 可以使用多线程化函数(包括JavaScript函数组)
    * 解决了JavaScript通常的域,可以ajax输入多线程化代码的参数.

名前空間(命名空间)

本库使用「Concurrent」作为命名空间,「Thread」定义为构造器.本库的其他名称都在「Concurrent.Therad」下定义.
Concurrent.Thread 对象(object)

virtual-thread表示对象.包括生成线程的静态方法,操作线程的方法.
継承(继承)

    * → Object

static方法
prepare( func )

返回函数的javascript的代码.
compile( func )

virtual-thread 执行func.返回func执行结果.和下面的代码相同.

eval(Concurrent.Thread.prepare(func))

create( func, arg1, arg2, ... )

新建一个 virtual-thread,执行这个func.arg1, arg2, ... 为传入func的参数.和下面的代码相同.

Concurrent.Thread.compile(func).async(null, [arg1, arg2, ...])

返回 Concurrent.Thread对象.
self( )

获取当前 Concurrent.Thread 对象.virtual-thread化的,native-thread(通常的script)执行是返回null.
sleep( t )

当前运行的 virtual-thread 暂停 t ms.使用 notify方法可以中断暂停.
stop( )

使当前运行的virtual-thread进入停止状态.使用 notify方法恢复运行.下面是基本的用法.

try { Concurrent.Thread.stop();}catch ( e ) { if ( e instanceOf Concurrent.Thread.KillException ) { throw e; } else if ( e instanceof SomeOtherException ) { do_something(); } . . .}

yield( )

非独占CPU.和下面相等.

Concurrent.Thread.sleep(0)

instance  接口方法
th.join( )

th线程执行完后,当前的virtual-thread 待机.thが実行した関数の戻り値を返す.下記の notify でキャンセルできる.後述の「例外の伝播」も参照.

戻り値としてスレッドthの上で実行された関数の戻り値を返す.
th.kill( s )

和下面的相同.

th.notify( new Concurrent.Thread.KillException(s) )

th.notify( e )

在线程th中抛出异常.解除th中sleep,stop,join的待机状态.
Concurrent.Thread.KillException オブジェクト

从线程外部强制结束线程.与通常的相同可以使用 catch来取消.
継承

    * → Data.Error.Exception
    * → Error
    * → Object

instance属性
thread

例外の発生元である Concurrent.Thread オブジェクトへの参照を保持する.
Function 对象扩展
instance方法
f.async(this_val, args)

新建线程,调用函数f.this_val为this,args为参数数组.

返回一个Concurrent.Thread对象.
分享到:
评论
1 楼 ywxowen999 2011-09-19  
这位兄弟,不知道你研究过Concurrent.Thread的源码没?我看你对此有所研究,想请教一个问题。Concurrent.Thread的源代码中出现大量的with嵌套,代码如下:
(function() {
	
	if ( !this.Math || ( typeof this.Math != 'object' && typeof this.Math != 'function' ) )
		this.Math = new Object();
	if ( this.Math.ToInteger === undefined )
		this.Math.ToInteger = undefined;
	if ( this.Math.ToInt32 === undefined )
		this.Math.ToInt32 = undefined;
	if ( this.Math.ToUInt32 === undefined )
		this.Math.ToUInt32 = undefined;
	if ( this.Math.ToUInt16 === undefined )
		this.Math.ToUInt16 = undefined;
		
	with ( function(){
		with ( Math ) {
	
			return function () {
				var VERSION = '0.0.0';
				var NAMESPACE;
			
				NAMESPACE = 'Math';
			
				// This module provides functions emulating the integral type
				// conversions defined in ECMA-262 3rd.
			
				function ToInteger ( n ) {
					return n < 0 ? ceil(n)
					: floor(n) || 0;
				}
			
				function ToInt32 ( n ) {
					return n | 0;
				}
			
				function ToUInt32 ( n ) {
					return n >>> 0;
				}
			
				function ToUInt16 ( n ) {
					return n & 0xFFFF;
				}
			
				return {
					'ToUInt16': ToUInt16,
					'ToUInt32': ToUInt32, 
					'ToInt32': ToInt32, 
					'ToInteger': ToInteger
				};
			}();
		}
	}.call(null) ) {
		this.Math.ToUInt16 = ToUInt16;
		this.Math.ToUInt32 = ToUInt32;
		this.Math.ToInt32 = ToInt32;
		this.Math.ToInteger = ToInteger;
	}
}).call(null);

我不明白为什么他需要这样写,有什么优点?
不能按照下面这种方式来写嘛?
(function() {
	
	if ( !this.Math || ( typeof this.Math != 'object' && typeof this.Math != 'function' ) )
		this.Math = new Object();
	if ( this.Math.ToInteger === undefined )
		this.Math.ToInteger = undefined;
	if ( this.Math.ToInt32 === undefined )
		this.Math.ToInt32 = undefined;
	if ( this.Math.ToUInt32 === undefined )
		this.Math.ToUInt32 = undefined;
	if ( this.Math.ToUInt16 === undefined )
		this.Math.ToUInt16 = undefined;
		
	this.Math.ToUInt16 = function ToUInt16 ( n ) {
					return n & 0xFFFF;
	}	
	
	this.Math.ToUInt32 = function ToInt32 ( n ) {
		return n | 0;
	}	
	
	this.Math.ToInt32 = function ToInt32 ( n ) {
		return n | 0;
	}
	
	this.Math.ToInteger = function ToInteger ( n ) {
		return n < 0 ? ceil(n) : floor(n) || 0;
	}
	
}).call(null);

相关推荐

Global site tag (gtag.js) - Google Analytics