`
hanyi366
  • 浏览: 284968 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

async-threading

    博客分类:
  • Flex
 
阅读更多

 

开源项目async-threading能使as3支持多线程,google code地址:http://code.google.com/p/async-threading/

 

要在flex4 sdk环境下使用,要先修改一下源代码,打开com.symantec.premiumServices.asyncThreading.handlers.FPSObserverHandler

 

      将import mx.core.Application; 修改为import spark.components.Application;

  

      将private var _appRef:Application = Application.applicationas Application;修改为private var _appRef:Application = FlexGlobals.topLevelApplication as Application;

 

      同时导入import mx.core.FlexGlobals;

 

      这个api要求自定义的线程继承AbstractAsyncThread然后实现IAsyncThreadResponder接口,写一个测试用线程:

 

package thread
{
	import com.symantec.premiumServices.asyncThreading.abstract.AbstractAsyncThread;
	import com.symantec.premiumServices.asyncThreading.interfaces.IAsyncThreadResponder;
	
	/**
	 *  自定义线程 
	 */
	public class SelfDefinedThread extends AbstractAsyncThread implements IAsyncThreadResponder
	{
		private var f:Function;
		
		public function SelfDefinedThread(f:Function)
		{
			super();
			this.f = f;
		}
		
		public function execute():void
		{
			f.call();
		}
	}
}
 

 


       这个api还是比较强大的,基本线程操作和通讯都能实现了,具体可以看附件内的源代码api。

 

      这个线程的构造函数要传入一个方法,这个方法将在线程启动后调用。

 

      测试代码:

 

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
					   xmlns:s="library://ns.adobe.com/flex/spark" 
					   xmlns:mx="library://ns.adobe.com/flex/mx"
					   creationComplete="testThread()">
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	
	<fx:Script>
		<![CDATA[
			import thread.SelfDefinedThread;
			
			private function testThread():void{
				//线程1
				var thread1:SelfDefinedThread = new SelfDefinedThread(
					function():void{
						trace("线程1执行");
						trace("开启线程2");
						thread2.start();
					});
				
				//线程2
				var thread2:SelfDefinedThread = new SelfDefinedThread(
					function():void{
						trace("线程2执行");
						if(thread1.sleeping)
						{
							thread1.kill();
							thread2.kill();
							thread3.kill();
							trace("线程1,2,3终止");
						}else{
							trace("开启一个线程3");
							thread3.start();
						}	
					});
				
				//线程3
				var thread3:SelfDefinedThread = new SelfDefinedThread(
					function():void{
						trace(thread3.id + "线程3执行");
						if(!thread1.sleeping)
						{
							thread1.sleep();
							trace("线程1休眠");
						}
					});
				thread1.priority = thread1.RUN_LEVEL_HIGH;
				thread2.priority = thread2.RUN_LEVEL_BELOW_NORMAL;
				thread1.start();
			}
			
		]]>
	</fx:Script>
	<s:Label x="281" y="122" text="多线程测试"/>
</s:WindowedApplication>

  用调试模式执行就能看到结果:

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics