background preloader

AS3Workers

Facebook Twitter

AS3 Concurrency/Workers use cases, best practices, and link roundup. Leif Wells responded to my presentation slides on ActionScript 3 Workers/Concurrency with a great question: Is there a discussion somewhere on practical use cases for AS3 Workers? Best Practices? (Leif Wells on Twitter) Use cases I talked some about practical use cases in my presentation. Text/data processing (e.g. the example I gave in the slides of loading an xml file and looping over all the nodes to create an object graph)visual/image/audio data processing (e.g. any image processing algorithm that isn’t already using PixelBender) – some examples are in the links belowPhysics engine running in the background Best practices As for “best practices” that’s basically what I talked about in the section called “caveats” in slides 69-71.

Under normal circumstances, you shouldn’t use more than one or two background workers at most. Links and resources Adobe documentation Using workers for concurrency from the ActionScript 3.0 Developer’s Guide. Examples/Tutorials. Flex 4: A multi threading solution. If you develop applications in Silverlight a lot, it becomes more and more annoying that Flex doesn’t support multi threading. This is particularly annoying when you are processing a long running task and need to display some progress. The problem is that you can’t do this task on a background thread, this means that the ui thread is busy processing your task and it can’t update the ui until your task is done, at which the progress of course is 100%. In this article I’m going to show how you can somewhat work around Flex’ and more precisely Flash’ lack of multithreading while still being able to let the ui be responsive and show progress.

The sample Flex application The sample application in this blog post is a Flex application which turns an image into greyscale. A better option for this would be to use a custom filter built with PixelBender, but this example demonstrates the problem quite well and with PixelBender you lose the ability to report progress. 1: <? 8: <fx:Script> 1: package. Slides and code: Concurrent programming with ActionScript workers. Several weeks ago, I gave a presentation to the D-Flex users’ group about the new ActionScript workers features in Flash Player 11.4/AIR 3.4. Although I’m a few weeks behind on this, I wanted to share my slides and code. Update Nov. 7 2012: On recommendation from Leif Wells, I realized it would be useful to share some links and tips on use cases and best practices for AS3 Workers. A lot of the presentation involved talking through examples in the slides, but I also showed a “full” code example at the end.

If you’re interested you can view and download the example from GitHub. It’s a simple app that just counts from 0 to 100,000,000 in a loop. The counting happens behind the scenes – it’s not rendered on the screen – but it does have a progress bar and progress status text field that are updated as the counting happens: Near the start of the presentation I showed a version of the app that does things the “wrong” way (on GitHub this is called the “NonWorkerVersion”). FlexMonkey: Workers. Intro to AS3 Workers (Part 1): Hello World. [UPDATE]: Part 2 and Part 3 of this tutorial are available now [UPDATE 2]: Adobe has removed support for ByteArray.shareable until Player 11.5.

With the beta release of AIR 3.4 and Flash 11.4, Adobe has introduced one of the most requested API’s for years: Multi-threading! With AS3 workers it’s now very easy to create true multi-threaded applications with just a few lines of code. The API is fairly straightforward, and they’ve done some very handy things like a new ByteArray.shareable property to share memory between worker’s, and a new BitmapData.copyPixelsToByteArray API for quickly converting bitmapData to ByteArray.

In this post I’ll go through the various components of the Worker API, and we’ll look at a simple little HelloWorker application. If you would like to follow along, you can download the FlashBuilder Project Files: FlashBuilder Project Files If you just want to see the code, without any crap: Check out HellWorldWorker.as There’s currently 3 ways to generate these SWF bytes: Probertson/as3-workers-examples.