As3可以使用多线程加载资源,要求11.4以上,需要导入easyWorker.swc。
下面的例子是子线程上捕获计算机的时间,并将其发送到主线程上并显示:
子线程:
package worker
{ import flash.display.Sprite; import flash.events.Event; import flash.events.TimerEvent; import flash.net.registerClassAlias; import flash.system.MessageChannel; import flash.system.Worker; import flash.utils.Timer; import worker.vo.TimeVo; /** * ... * @author me */ public class TimeWorker extends Sprite { private var _commandChannel:MessageChannel; private var _returnChannel:MessageChannel; public function TimeWorker() { init(); } private function init():void { registerClassAlias("worker.vo.TimeVo", TimeVo); _commandChannel = Worker.current.getSharedProperty("commandChannel") as MessageChannel; _commandChannel.addEventListener(Event.CHANNEL_MESSAGE, onMessageChannel); _returnChannel = Worker.current.getSharedProperty("returnChannel") as MessageChannel; } private function onMessageChannel(e:Event):void { if (!_commandChannel.messageAvailable) { return; } var message:Array = _commandChannel.receive() as Array; if (message && message[0] == "start") { startTime(); } } private var _timer:Timer; private function startTime():void { if (!_timer) { _timer = new Timer(1); _timer.addEventListener(TimerEvent.TIMER, onTimer); _timer.start(); } } private function onTimer(e:TimerEvent):void { var date:Date = new Date(); var timeVo:TimeVo = new TimeVo(); timeVo.fromDate(date); _returnChannel.send(timeVo); } }}
主线程:
package worker
{ import flash.display.Sprite; import flash.events.Event; import flash.net.registerClassAlias; import flash.system.MessageChannel; import flash.system.Worker; import flash.system.WorkerDomain; import flash.system.WorkerState; import flash.text.TextField; import flash.text.TextFormat; import flash.utils.ByteArray; import worker.vo.TimeVo; /** * ... * @author me */ public class BaseWorker extends Sprite { [Embed(source = "E://HelloWorld/New Project/bin/TimeWorker.swf", mimeType = "application/octet-stream")] private static var TimeWorkerSwf:Class; private var _timeWorker:Worker; private var _timeWorkerCommandChannel:MessageChannel; private var _returnChannel:MessageChannel; private var _timeTxt:TextField; public function BaseWorker() { init(); } private function init():void { registerClassAlias("worker.vo.TimeVo", TimeVo); _timeWorker = WorkerDomain.current.createWorker(timeWorkerBytes); _timeWorkerCommandChannel = Worker.current.createMessageChannel(_timeWorker); _timeWorker.setSharedProperty("commandChannel", _timeWorkerCommandChannel); _returnChannel = _timeWorker.createMessageChannel(Worker.current); _returnChannel.addEventListener(Event.CHANNEL_MESSAGE, onMessageChannel); _timeWorker.setSharedProperty("returnChannel", _returnChannel); _timeWorker.addEventListener(Event.WORKER_STATE, onWorkerState); _timeWorker.start(); } private function initTxt():void { _timeTxt = new TextField(); _timeTxt.width = 200; var tf:TextFormat = new TextFormat(); tf.font = "微软雅黑"; tf.size = 16; _timeTxt.defaultTextFormat = tf; _timeTxt.x = stage.stageWidth / 2 - 100; _timeTxt.y = stage.stageHeight / 2; addChild(_timeTxt); } private function onWorkerState(e:Event):void { if (_timeWorker.state == WorkerState.RUNNING) { _timeWorkerCommandChannel.send(["start"]); } } private function onMessageChannel(e:Event):void { var vo:TimeVo = _returnChannel.receive() as TimeVo; if (vo) { if (!_timeTxt) { initTxt(); } _timeTxt.text = vo.toString(); } } private static function get timeWorkerBytes():ByteArray { return new TimeWorkerSwf(); } }}
TimeVo:
package worker.vo
{ import utils.NumTool; /** * ... * @author me */ public class TimeVo { private var _year:int; private var _month:int; private var _day:int; private var _hour:int; private var _minuter:int; private var _second:int; private var _millSecond:int; public function TimeVo(year:int = 0, month:int = 0, day:int = 0, hour:int = 0, minuter:int = 0, second:int = 0, millSecond:int = 0) { this.year = year; this.month = month; this.day = day; this.hour = hour; this.minuter = minuter; this.second = second; this.millSecond = millSecond; } public function get year():int { return _year; } public function set year(value:int):void { _year = value; } public function get month():int { return _month; } public function set month(value:int):void { _month = value; } public function get day():int { return _day; } public function set day(value:int):void { _day = value; } public function get hour():int { return _hour; } public function set hour(value:int):void { _hour = value; } public function get minuter():int { return _minuter; } public function set minuter(value:int):void { _minuter = value; } public function get second():int { return _second; } public function set second(value:int):void { _second = value; } public function get millSecond():int { return _millSecond; } public function set millSecond(value:int):void { _millSecond = value; } public function fromDate(date:Date):void { this.year = date.getFullYear(); this.month = date.getMonth() + 1; this.day = date.getDate(); this.hour = date.getHours(); this.minuter = date.getMinutes(); this.second = date.getSeconds(); this.millSecond = date.getMilliseconds(); } public function toString():String { return year + "." + NumTool.fill(month, 2) + "." + NumTool.fill(day, 2) + " " + NumTool.fill(hour, 2) + ":" + NumTool.fill(minuter, 2) + ":" + NumTool.fill(second, 2) + ":" + this.millSecond; } }}
注意:先编译子线程生成TimeWorker.swf,再嵌入主线程中