AS3 Multithreading Quick Start (i): Hello World

Source: Internet
Author: User

Original: http://blog.domlib.com/articles/319

With beta releases of AIR3.4 and Flash Player11.4, Adobe has finally launched the API that has been asked the most over the years: multithreading.

Today, using AS3 workers makes it easy to create real multi-threaded applications, with just a few lines of code. This API is fairly straightforward, And they do some very useful things: such as bytearray.shareable this attribute, which can be used to share memory between the worker, and a new Bitmapdata.copypixelstobytearray method to quickly convert BitmapData to byte Array.

In this article, I'll go through all the components of this API workers. Then we'll look at a simple little program: Helloworker.

If you want to follow up, you can download the following Flash Builder project file:
Flashbuilder Project Files

If you just want to look at the code, nonsense is not much to say, download this look: hellworldworker.as

What is that worker?

Simply put, a worker is another SWF program that runs in your main SWF. To get a deeper understanding, please go to: Thibault Imbert this New Wen.

To create a worker, you need to call the WorkerDomain.current.createWorker () method and pass in a byte stream for a SWF file.

There are 3 different ways to generate those SWF byte streams.

1. Use the Loaderinfo.bytes property of the main SWF application itself, and then judge the Worker.current.isPrimordial flag in the constructor of the document class (true to represent the current main thread). This is the quickest way to create a worker, and it has an advantage in the Just-in-time debugging cycle.

2. Publish a SWF file and embed it into your main program with the [Embed] tag. This approach will have tool support in Flash Builder4.7, but it is still too inflexible before the tool comes out. Every time you change the code in the worker, you have to export the SWF once again, which is quickly annoying.

Using the worker from class, this new class library allows you to create workers directly from the class. This may seem like the best solution, but it allows your project to introduce a range of external dependencies.

In this initial tutorial, I will focus on the first way to use my own loaderinfo.bytes byte stream. It's a quick but flawed way. In the next tutorial, I'll try the "Worker from class" Class library approach.

For the 2nd way, there's already a very good tutorial that you can take a look at Lee Brimelow's video Tutorials Part 1 and Part 2.

let's talk.

Communication is critical in any multithreaded usage scenario. The cost of transferring memory data directly from one thread to another is expensive, and memory sharing requires careful design and thoughtful consideration. In the process of implementing a worker system, most of the challenges come from finding the right design architecture that allows data to be shared among your worker.

To help us solve this problem, Adobe has given us some simple (but flexible) ways to send data.

(1) worker.setsharedproperty () and Worker.getsharedproperty ()

This is the simplest but most functional and limited way to pass data. You can call Worker.setsharedproperty ("key", value) to set the data and then use WorkerDomain.current.getSharedProperty ("key") to get them on the other side. Sample code:

?
 
 
1 2 3 4 5 In the main line Chengri worker.setsharedproperty ("foo", true); In the Worker line Chengri var foo:boolean = Worker.current.getSharedProperty ("foo");

You can store simple or complex objects here, but for most cases, the stored data is serialized, and it's not really shared. If a data object changes on one side, the other side does not sync updates until you call the Set/get method again.

There are two exceptions that can be shared if you pass an object that is ByteArray and the Shareable property is true, or is a Messagechannel object. Coincidentally, these are just the other two ways of communication.

(2) Messagechannel

Messagechannels is like a one-worker to another one-way pipe. They combine the event mechanism with the simple queuing system. You call the Channel.send () method at one end and a Event.channel_message event is thrown at the other end. In the handler for the event, you can call Channel.receive () to receive the data sent over. As I mentioned, this method is similar to a queue. So, you can send () or receive () multiple times on either side. Sample code:

    1 2     

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.