ActionScript Worker Demo

来源:互联网 发布:unity3d ui插件 编辑:程序博客网 时间:2024/06/08 17:10

一直以来都期盼as3什么时候能够支持多线程,现在终于来了,特写了一个小demo,照搬别人的,helloworld级的,羞愧,····

001package
002{
003    import flash.display.Sprite;
004    import flash.events.Event;
005    import flash.system.MessageChannel;
006    import flash.system.Worker;
007    import flash.system.WorkerDomain;
008    import flash.utils.setInterval;
009 
010    public class WorkerTest extends Sprite
011    {
012        protected var mainToWorker:MessageChannel;
013        protected var workerToMain:MessageChannel;
014        protected var worker:Worker;
015 
016        public function WorkerTest()
017        {
018            this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
019        }
020 
021        private function onAddedToStage(e:Event) : void
022        {
023            this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
024 
025            init();
026        }
027 
028        private function init() : void
029        {
030            if (Worker.current.isPrimordial)
031            {
032                worker = WorkerDomain.current.createWorker(this.loaderInfo.bytes);
033 
034                mainToWorker = Worker.current.createMessageChannel(worker);
035                workerToMain = worker.createMessageChannel(Worker.current);
036 
037                worker.setSharedProperty("mainToWorker", mainToWorker);
038                worker.setSharedProperty("workerToMain", workerToMain);
039 
040                workerToMain.addEventListener(Event.CHANNEL_MESSAGE, onWorkerToMain);
041 
042                worker.start();
043 
044                setInterval(
045                    function () : void
046                    {
047                        //helloworld();
048                        count();
049                    },
050                    2000
051                );
052            }
053            else
054            {
055                mainToWorker = Worker.current.getSharedProperty("mainToWorker");
056                workerToMain = Worker.current.getSharedProperty("workerToMain");
057 
058                mainToWorker.addEventListener(Event.CHANNEL_MESSAGE, onMainToWorker);
059            }
060        }
061 
062        protected function helloworld() : void
063        {
064            mainToWorker.send("HELLO");
065            trace("[Main] HELLO ");
066        }
067 
068        protected function count() : void
069        {
070            mainToWorker.send("ADD");
071            mainToWorker.send(2);
072            mainToWorker.send(5);
073 
074            trace("[Main] ADD 2 + 5 ?");
075        }
076 
077        protected function onMainToWorker(e:Event) : void
078        {
079            var msg:* = mainToWorker.receive();
080 
081            switch (msg)
082            {
083                case "HELLO":
084                    workerToMain.send("WORLD");
085                    break;
086                case "ADD":
087                    var a:int = mainToWorker.receive();
088                    var b:int = mainToWorker.receive();
089 
090                    workerToMain.send(a + b);
091                    break;
092            }
093        }
094 
095        protected function onWorkerToMain(e:Event) : void
096        {
097            trace("[Worker] " + workerToMain.receive());
098        }
099    }
100}

result:

1[Main] ADD 2 + 5 ?
2[Worker] 7
0 0