最近在网上看到一个工具类可以提供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);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?