现在的位置: 首页 > 综合 > 正文

Flex中多线程的实现

2013年10月17日 ⁄ 综合 ⁄ 共 4235字 ⁄ 字号 评论关闭

最近在网上看到一个工具类可以提供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()杀掉线程
还提供AsyncThreadingManager来管理所有的进程
提供的方法:
    
   
  shutDown()停掉所有的线程
    
   
  killAllThreads()杀掉所有的线程
还支持线程之间通信,利用sendMessageToThreads(name:String,body:Object):Boolean

抱歉!评论已关闭.