Flex中多線程的實現

來源:互聯網
上載者:User

最近在網上看到一個工具類可以提供flex中多線程的支援,AsyncThreading,googleCode地址,詳細文檔可以去googleCode上看 
下面總結一下利用這個工具類來實現一個抽獎機的demo。
首先我們要編寫一個自訂線程類,繼承自AbstractAsyncThread並且實現IAsyncThreadResponder介面,
AbstractAsyncThread這個類可以控制線程的層級:

                 public const RUN_LEVEL_REAL_TIME:int = 1;
                public const RUN_LEVEL_HIGH:int = 3;
                public const RUN_LEVEL_ABOVE_NORMAL:int = 6:
                public const RUN_LEVEL_NORMAL:int = 8;
                public const RUN_LEVEL_BELOW_NORMAL:int = 12:
                public const RUN_LEVEL_LOW:int = 24;  

IAsyncThreadResponder這個介面提供了一個線程啟動後調用的方法

                 function execute():void; 

自訂線程對象類代碼如下:

 package threads
{
        import cn.ningyu.utils.Random;
       
        import com.symantec.premiumServices.asyncThreading.abstract.AbstractAsyncThread;
        import com.symantec.premiumServices.asyncThreading.interfaces.IAsyncThreadResponder;
       
        import mx.controls.Label;
        public class WorkBee extends AbstractAsyncThread implements IAsyncThreadResponder
        {
                private var _lab:Label;
                public function WorkBee(lab:Label)
                {
                        _lab = lab;
                        super();
                        super.priority = super.RUN_LEVEL_REAL_TIME;
                }
               
                public function execute():void
                {
                        var random:Random = Random.getInstance();
                        random.digit = 1;
                        random.radix = Random.NUMBER;
                        _lab.text = random.createRandom();
                }
               
        }
}

application中代碼如下:

 <?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
       
        <mx:Script>
                <![CDATA[
                        import threads.WorkBee;
                        import com.symantec.premiumServices.asyncThreading.AsyncThreadingManager;
                        import mx.collections.ArrayCollection;
                       
                        private var threadList:ArrayCollection = new ArrayCollection();
               
                        private function init():void {
                                threadList = new ArrayCollection();
                                for(var i:int=0;i<7;i++) {
                                        var workBee:WorkBee;
                                        switch(i) {
                                                case 0:
                                                        workBee = new WorkBee(lab1);
                                                        break;
                                                case 1:
                                                        workBee = new WorkBee(lab2);
                                                        break;
                                                case 2:
                                                        workBee = new WorkBee(lab3);
                                                        break;
                                                case 3:
                                                        workBee = new WorkBee(lab4);
                                                        break;
                                                case 4:
                                                        workBee = new WorkBee(lab5);
                                                        break;
                                                case 5:
                                                        workBee = new WorkBee(lab6);
                                                        break;
                                                case 6:
                                                        workBee = new WorkBee(lab7);
                                                        workBee.wake()
                                                        break;
                                        }
                                        threadList.addItem(workBee);
                                }
                        }
                       
                        private function onClick():void {
                                if(btn.label == "開始抽獎") {
                                        for(var i:int=0;i<7;i++) {
                                                if(threadList[i].sleeping) {
                                                        threadList[i].wake();
                                                } else {
                                                        threadList[i].start();
                                                }
                                        }
                                        btn.label = "停止抽獎";
                                } else if(btn.label == "停止抽獎") {
                                        for(var i:int=0;i<7;i++) {
                                                threadList[i].sleep();
                                        }
                                        btn.label = "開始抽獎";
                                }
                        }
                       
                ]]>
        </mx:Script>
       
        <mx:FormItem verticalCenter="0" horizontalCenter="0" direction="horizontal" fontSize="50" fontWeight="bold">
                <mx:Label id="lab1"/>
                <mx:Label id="lab2"/>
                <mx:Label id="lab3"/>
                <mx:Label id="lab4"/>
                <mx:Label id="lab5"/>
                <mx:Label id="lab6"/>
                <mx:Label id="lab7"/>
                <mx:Button id="btn" label="開始抽獎" click="onClick()"/>
        </mx:FormItem>
</mx:Application>

注意的點:
線程方法:start()啟動線程
          sleep()休眠線程
          sleeping線程是否處在休眠狀態
          wake()重新啟用線程
          kill()殺掉線程
async-threading  
還提供AsyncThreadingManager來管理所有的進程
提供的方法:
           shutDown()停掉所有的線程
           killAllThreads()殺掉所有的線程
還支援線程之間通訊,利用sendMessageToThreads(name:String,body:Object):Boolean

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.