Skip to main content

Threads in Actionscript 3 (AS3)

by Alex Harui @ blogs.adobe.com. January 1, 2008

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.
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.
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.
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.
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.
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.
PseudoThread.as
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.
PseudoThreadTest.swf
These two links are the source file and the test image.
PseudoThreadTest.mxml
the test image
The usual caveats apply (there are probably bugs, limitations, etc). Hope it helps.

Comments

Popular posts from this blog

Powerful job portal with university web integration

Creating a powerful job portal with university web integration involves combining cutting-edge technology, strategic partnerships, and a user-centric approach to bridge the gap between academia and industry. In this comprehensive explanation, I'll delve into the key components and benefits of such a platform.

Langkah mudah membuat aplikasi sendiri di Facebook

konten diperbaharui karena sudah tidak relevan. Getting Started Websites Apps on Facebook Mobile Samples & How-Tos Videos Core Concepts Social Design Social Plugins Open Graph protocol Social Channels Authentication Graph API Advanced Topics Dialogs FQL Internationalization Ads API Credits API Chat API Legacy REST API Legacy FBML Legacy FBJS Legacy Javascript SDK SDKs & Tools Open Graph Beta -----iklan----- ----- lihat juga: >> Belajar Cara Membuat Software >> Contoh Source Code Aplikasi Facebook penasaran banget nih pingin nyobain bikin aplikasi di Facebook.com. perlu kalian tahu, kalo facebook udah menyediakan API yang bisa anda gunakan di aplikasi anda. fitur integrated seperti ini akan memberikan "kesan" seolah - olah kita tidak pernah beranjak dari facebook. projek sebuah aplikasi di facebook sepertinya akan menjadi hal yang menarik. Gath adam sudah membuat artikel tentang cara membuat apl

Solusi mengatasi Tindihan (Eureup Eureup)

Tindihan alias eureup eureup atau dalam bahasa inggris disebut juga dengan sleep paralysis adalah kondisi umum tubuh dengan ciri - ciri kelumpuhan sementara sebagian atau total dari otot rangka dan areflexia yang terjadi setelah bangun dari tidur atau tidak sepenuhnya terbangun ketika dalam keadaan tertidur [ref] . Saat pertama kali mengalami kejadian ini, saya merasa takut yang amat sangat ketika tubuh tidak bisa digerakkan ditambah dengan melihat citra - citra yang seharusnya tidak ada. Keringat pun banyak keluar di sekitar leher dan punduk setelah benar - benar bangun. Waktu itu saya tidak tahu menahu sedikitpun tentang hal seperti itu . itu terjadi ketika saya masih berumuran anak smp. Lama kelamaan, saya pun sudah mulai terbiasa seiring dengan informasi yang saya dapatkan tentang tindihan. Bahkan, saya suka bereksperimen agar bisa bangun tanpa harus merasa kelelahan jika terulang hal serupa. Ini yang saya lakukan ketika tindihan atau eureup eureup terjadi: Jangan ngot