【Adobe Consult】AS3线程

导读:
  Every once in a while, someone decides they would like to see threading or background processing in Actionscript. While the underlying Flash Player and Operating System use threads, the execution model for Actionscript does not.


有的人一时兴起,认为需要AS3也支持后台处理的线程。但是虽然FLASH PLAYTER以及OS支持使用线程,但是AS的执行模型并不支持线程模型
  For those who don't know, 'threading' is essentially the ability to have the code do something in the background while the UI is doing something else. Because Actionscript is single-threaded, if you spend lots of time doing heavy computation, the UI cannot be updated while you're doing that computation so your application appears stuck or effects don't run smoothly.


对于那些不了解的人来说,线程是必须的,如果我们要UI执行某个动作的时候在后台完成其他的任务。因为AS是单线程的所以如果你把很多的时间花在大量的计算上,那么你的UI得不到及时的更新,那么你的UI看起来就奇卡无比,或者是不顺畅
  Similarly, there is no yielding or blocking in Actionscript either. If the next line of code is supposed to run, you cannot prevent the next line of code from running. That means that when you call Alert.show(), the next line of code following that runs right away. In many other runtimes, the Alert window has to be closed before the next line of code continues.


同样的,也没有挂起或者是阻止的相关线程概念,如果下一行代码要执行,那么你就不能阻止这行代码的执行,例如你执行了ALERT.SHOW,那么它的下一行代码就会立即执行,然而在很多的其他系统里面。为了使得下一行代码得到执行,你必须首先关闭ALERT
  Threading may be a feature of Actionscript some day, but until then, you have to live with the fact that there is no such thing right now.


线程也许是AS将来的基本特性,但是在那之前,你必须接受AS是单线程的现实。 
  In other single-threaded environments, you can get something like threads via 'pseudo-threading', which involves dividing up the heavy computation into small chunks on your own. (There is no solution to allow for blocking or yielding other than refactoring the application to be even-driven). Pseudo-threading is painful, but doable in most situations. Iterative tasks are easier than recursive ones, the chunk size has to be small enough to not degrade the UI response time and restoration of the execution context must be efficient otherwise you'll spend too much time saving and restoring your state and not get anything done.


然而在单线程的世界里面,你可以通过PSEUDO-THREADING模式来模拟线程。例如将大量的计算任务分包成小块然后分别完成,不过对于挂起线程没有可以替代的解决方案,这种模式是令人痛苦的,但是通常是有效的,通常迭代要比递归容易的多,同时要注意的是计算块要划分到足够的小不能让其计算影响到UI的渲染
  Someone asked me how I would generalize such a thing. I put this together in a couple of hours. It represents my first thoughts, not some deep thinking on the subject. A PseudoThread instance takes a callback function and a context object and calls the callback function with the context object as mahy times as it thinks it can get away with it within the frame interval. The callback function should update the context object before returning and return false when done and the PseudoThread instance dispatches an event and destroys itself.


也有人问过我如何来实现这样的一个东西,我总共花了一个多小时来处理这个问题,当然只是一个初步的想法,不是成熟的考虑结果,这种模式需要一个环境对象,和一个回调函数,然后这个环境对象就可以多次的调用回调函数,并更新自己,可以设定调用的间隔,然后当回调函数返回FALSE的时候完成计算销毁对象。
  The following is the a test that loads an image, and adds a red tint to a copy of the image one row at a time. I purposefully slowed it down with trace statements so you can see that it doesn't do it all at once and doesn't seem to affect the rotating button.
下面是我的构想的一个例子


PseudoThread.as


 


PseudoThreadTest.swf


These two links are the source file and the test image.
PseudoThreadTest.mxml
the test image


The usual caveats apply
本文转自
http://blogs.adobe.com/aharui/2008/01/threads_in_actionscript_3.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值