views:

171

answers:

5

I've seen a lot of other developers refer to threads in ActionScript functions. As a newbie I have no idea what they are referring to so:

What is a thread in this sense?
How would I run more than one thread at a time?
How do I ensure that I am only running one thread at a time?

Thanks ~mike

+4  A: 

It really depends on what you mean. The execution model for ActionScript is single-threaded, meaning it can not run a process in the background.

If you are not familiar with threading, it is essentially the ability to have something executed in the background of a main process.

So, if you needed to do a huge mathematical computation in your flex/flash project, with a multi-threaded program you could do that in the background while you simultaneously updated your UI. Because ActionScript is not multi-threaded you can not do such things. However, you can create a pseudo-threading class as demonstrated here:

http://blogs.adobe.com/aharui/pseudothread/PseudoThread.as

sberry2A
+5  A: 

Threads represent a way to have a program appear to perform several jobs concurrently. Although whether or not the jobs can actually occur simultaneously is dependent on several factors (most importantly, whether the CPU the program is running on has multiple cores available to do the work). Threads are useful because they allow work to be done in one context without interfering with another context.

An example will help to illustrate why this is important. Suppose that you have a program which fetches the list of everyone in the phone book whose name matches some string. When people click the "search" button, it will trigger a costly and time-consuming search, which might not complete for a few seconds.

  • If you have only a single-threaded execution model, the UI will hang and be unresponsive until the search completes. Your program has no choice but to wait for the results to finish.

  • But if you have several threads, you can offload the search operation to a different thread, and then have a callback -- a trigger which is invoked when the work is completed -- to let you know that things are ready. This frees up the UI and allows it to continue to respond to events.

Unfortunately, because ActionScript's execution model doesn't support threads natively, it's not possible to get true threading. There is a rough approximation called "green threads", which are threads that are controlled by an execution context or virtual machine rather than a larger operating system, which is how it's usually done. Several people have taken a stab at it, although I can't say how widespread their usage is. You can read more at Alex Harui's blog here and see an example of green threads for ActionScript here.

John Feminella
Sounds a lot like Stackless Python's microthreads.
KingRadical
While AS3 code can't create true threads, the Flash Player itself is multi-threaded and many operations will cause actions to take effect on a separate thread, like a network call.
Sam
A: 

a thread allows you to execute two or more blocks of actionscrpt simultaniously by default you will always be executing on the same default thread unless you explcitly start a new thread.

Steve Psaltis
+2  A: 

Hey,

The others have described what threading is, and you'd need threading if you were getting hardcore into C++ and 3D game engines, among many other computationally-expensive operations, and languages that support multi-threading.

Actionscript doesn't have multi-threading. It executes all code in one frame. So if you create a for loop that processes 100,000,000 items, it will cause the app to freeze. That's because the Flash Player can only execute one thread of code at a time, per frame.

You can achieve pseudo-threading by using:

  • Timers
  • Event.ENTER_FRAME

Those allow you to jump around and execute code.

Tween engines like TweenMax can operate on 1000's of objects at once over a few seconds by using Timers. You can also do this with Event.ENTER_FRAME. There is something called "chunking" (check out Grant Skinner's AS3 Optimizations Presentation), which says "execute computationally expensive tasks over a few frames", like drawing complex bitmaps, which is a pseudo-multi-threading thing you can do with actionscript.

A lot of other things are asynchronous, like service calls. If you make an HTTPService request in Flex, it will send a request to the server and then continue executing code in that frame. Once it's done, the server can still be processing that request (say it's saving a 30mb video to a database on the server), and that might take a minute. Then it will send something back to Flex and you can continue code execution with a ResultEvent.RESULT event handler.

So Actionscript basically uses:

  • Asynchronous events, and
  • Timers...

... to achieve pseudo-multi-threading.

viatropos
A: 

I have used this for some background processing that I needed to take care of. It worked fairly well.

http://code.google.com/p/async-threading/

unscene