加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flash多线程学习资料导航

发布时间:2020-12-15 07:13:29 所属栏目:百科 来源:网络整理
导读:FP11.4的新功能竟然是多线程~~囧~~ 多线程资讯 多线程入门教程系列 下面copy一篇代码以做学习之资~~ package{import flash.display.Sprite;import flash.events.Event;import flash.system.MessageChannel;import flash.system.Worker;import flash.system.W

FP11.4的新功能竟然是多线程~~囧~~
  • 多线程资讯
  • 多线程入门教程系列
下面copy一篇代码以做学习之资~~

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.MessageChannel;
	import flash.system.Worker;
	import flash.system.WorkerDomain;
	import flash.utils.setInterval;
	
	public class HelloWorldWorker extends Sprite
	{
		protected var mainToWorker:MessageChannel;
		protected var workerToMain:MessageChannel;
		
		protected var worker:Worker;
		
		public function HelloWorldWorker()
		{
			/** 
			 * Start Main thread
			 **/
			if(Worker.current.isPrimordial){
				//Create worker from our own loaderInfo.bytes
				worker = WorkerDomain.current.createWorker(this.loaderInfo.bytes);
				
				//Create messaging channels for 2-way messaging
				mainToWorker = Worker.current.createMessageChannel(worker);
				workerToMain = worker.createMessageChannel(Worker.current);
				
				//Inject messaging channels as a shared property
				worker.setSharedProperty("mainToWorker",mainToWorker);
				worker.setSharedProperty("workerToMain",workerToMain);
				
				//Listen to the response from our worker
				workerToMain.addEventListener(Event.CHANNEL_MESSAGE,onWorkerToMain);
				
				//Start worker (re-run document class)
				worker.start();
				
				
				//Set an interval that will ask the worker thread to do some math
				setInterval(function(){
					mainToWorker.send("ADD");
					mainToWorker.send(2);
					mainToWorker.send(2);
				},1000);
					
			} 
			/** 
			 * Start Worker thread 
			 **/
			else {
				
				//Inside of our worker,we can use static methods to 
				//access the shared messgaeChannel's
				mainToWorker = Worker.current.getSharedProperty("mainToWorker");
				workerToMain = Worker.current.getSharedProperty("workerToMain");
				//Listen for messages from the mian thread
				mainToWorker.addEventListener(Event.CHANNEL_MESSAGE,onMainToWorker);
			}
		}
				
		//Main >> Worker
		protected function onMainToWorker(event:Event):void {
			var msg:* = mainToWorker.receive();
			//When the main thread sends us HELLO,we'll send it back WORLD
			if(msg == "HELLO"){
				workerToMain.send("WORLD");
			}
			else if(msg == "ADD"){
				//Receive the 2 numbers and add them together
				var result:int = mainToWorker.receive() + mainToWorker.receive();
				//Return the result to the main thread
				workerToMain.send(result);
			}
		}
		
		//Worker >> Main
		protected function onWorkerToMain(event:Event):void {
			//Trace out whatever message the worker has sent us.
			trace("[Worker] " + workerToMain.receive());
		}
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读