[Tips] as3 multi-thread Quick Start (1): Hello World

Source: Internet
Author: User

With the release of air3.4 and flash 11.4 beta versions, Adobe has finally launched the API that has been most required for years:Multithreading!

Today, using as3 workers makes it very easy to create a real multi-threaded application, just a few linesCodeYou can. This API is equivalent to sharing memory between workers. There is also a new bitmapdata. copypixelstobytearray method to quickly convert bitmapdata to bytearray.

In this article, I will go through all components of the workers API. Then let's look at a simple little bit.ProgramHelloworker.

I. What is a worker?

Simply put, worker is another SWF program that you run in the main SWF. To create a worker, you need to callWorderdomain. Current. createworker() Method and pass in the byte stream of a SWF file. There are three methods to generate those SWF byte streams.

1. Use the main SWF application's ownLoaderinfo. bytesProperties, and then judge in the document class ConstructorWorder. Current. isprimordialFlag (true indicates that the current thread is the main thread ). This is the fastest way to create a worder. It also has advantages in real-time call cycles.

2. Publish a SWF file and use the [embed] label to embed it into your main program. This method will be supported by tools in Flash builder4.7, but this method is not flexible until the tool has not been used. Every time you change the code in the worker, you have to export a new SWF, which is very annoying.

3. UseWorker from ClassThis new class library allows you to directly create workers from the class. This seems to be the best solution, but it introduces a series of external dependencies to your project.

In this initial tutorial, I will focus on the first way to use my own loaderinfo. bytes byte stream. This is a fast but flawed method. In the next tutorial, I will try the "worker from Class" class library method.

There is already a good tutorial for the 2nd methods. You can check out Lee brimelow's video tutorial.Part 1AndPart 2.

Let's talk about it.
In anyMultithreadingIn use cases, communication is critical. It is expensive to directly transmit memory data from one thread to another, and Memory sharing must be carefully designed and carefully considered. In the process of implementing the worker system, most of the challenges come from finding a correct design architecture that allows data to be shared among your workers.

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

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

This is the simplest way to transmit data, but the most functional. You can call worker. setsharedproperty ("key", value) to set data, and then use workerdomain. Current. getsharedproperty ("key") on the other side to get them. Sample Code:

// Worker. setsharedproperty ("foo", true) in the main thread; // var FOO: Boolean = worker. Current. getsharedproperty ("foo") in the worker thread ");

 

You can store simple or complex objects here, but in most cases, the stored data is serialized and not shared. If a data object changes on one side, it will not be updated synchronously on the other side until you call the set/get method again.
Two exceptions can be shared. If the object you pass isBytearray and the retriable attribute is trueOrMessagechannelObject. Coincidentally, these are the other two communication methods.

(2) messagechannel
Messagechannels is like a worker to another one-way channel. They work together with the event mechanism and simple queue system. You call the channel. Send () method at one end, and an event. channel_message event will be thrown at the other end. In the event processing function, you can call Channel. Receive () to receive sent data. As I mentioned, this method is similar to queue. Therefore, you can send () or receive () multiple times on both sides. Sample Code:

 

// Maintoworker in the main thread. send ("add"); maintoworker. send (2); maintoworker. send (2); // function onchannelmessage (Event: Event): void {var MSG: String = maintoworker in the worker thread. receive (); If (MSG = "add") {var val1: Int = workertomain. receive (); var val2: Int = workertomain. receive (); var result: Int = val1 + val2; // send the calculation result to the main thread workertomain (result );}}

Messagechannels objects use worker. setsharedproperty () for sharing. Therefore, you can share it among multiple workers. However, each of them can have only one sending endpoint. In this way, it seems nice to name the conventions with their recipients. For example, channeltoworker or channeltomain.
Sample Code:

// Workertomain = worker in the main thread. createmessagechannel (worker. current); worker. setsharedproperty ("workertomain", workertomain); maintoworker = worker. current. createmessagechannel (worker); worker. setsharedproperty ("maintoworker", maintoworker); // workertomain = worker in the worker thread. current. getsharedpropert ("workertomain"); maintoworker = worker. current. getsharedpropert ("maintoworker ");

 

Both messagechannel and sharedproperties have an important restriction on the communication mode. They are serialized when data is sent. This means they need to be parsed, transmitted, and restored at the other end. The performance overhead is relatively large. Due to this limitation, it is best to use these two communication modes to transmit small-scale data in an intermittent manner.

What if you want to share a large data block? Is to use the shared bytearray.

(3) bytearray. retriable
The fastest way to transmit data is not to transmit it!
Thanks to Adobe for providing us with a way to directly share bytearray objects. This is very powerful, because we can store almost any data in bytearray. To share a bytearray object, you need to set bytearray. retriable = true, and then call the messagechannel. Send (bytearray) or worker. setsharedpropert ("bytearray", bytearray) method to share it.
Once your bytearray object is shared, you can directly write data to it and then directly read it from the other end. That's great.
Basically, this is all the communication methods. As you can see, there are three completely different ways to share data. You can use them in a variety of ways. The infrastructure has passed through. Now let's take a look at a simple hello world example.

 

 

 

 

 

 

 

 

[Original address]

As3 multi-thread Quick Start (1): Hello World
Http://bbs.9ria.com/forum.php? MoD = viewthread & tid = 144587 & fromuid = 273124

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.