开发者

Actionscript 3 (as3) concurrency model

开发者 https://www.devze.com 2023-02-10 00:22 出处:网络
I have some evidence of a race condition [Note: see update below] in an Actionscript 3 (as3) program in which a certain object has methods invoked:

I have some evidence of a race condition [Note: see update below] in an Actionscript 3 (as3) program in which a certain object has methods invoked:

  1. When it handles asynchronous download events
  2. From a script added to a frame on a movieclip's timeline

What is the concurrency model in AS3? Is event handling serialised (i.e. each handler runs to completion for one event after the other, despite any underlying multi-threading)? And are scripts in frames serialised with that event stream? I suspect that the answer to that last question is negative.

I'm taking steps to log systematically whether/when a race condition occurs but it would be good to know what guarantees (if any) AS3 provides.

UPDATE: I did a simple check in my event handlers to record whether an event handler was already being processed on the particular object of interest and I found that that does occur, i.e. race conditions 开发者_运维知识库are possible. Has anyone else encountered this and what did you do about it?!


There is no "real" concurrency in Flash - everything, including the handling of events, is clocked by the main timeline. All your operations are certain to execute in the exact order specified in the code.

But of course, problems might occur if you base the result of one "asynchronously started" (I'm going to use this term, although it is not quite correct in a strict sense) block of operations on the outcome of another one - which can be the case when using event handlers. You will have to find some way to solve these issues yourself programmatically; there are no built-in constructs like synchronized or atomic, no threads and locking, or any other such mechanisms in ActionScript.

There are ways to simulate multithreading in Flash, and this tutorial might be an interesting way to start your search for a solution, as it explains some underlying concepts.


There is only one thread that your AS3 code can run in.

Race conditions are possible in handlers for asynchronous events, but not in the same sense as in a multi-threaded application, and are generally easier to track down.

For example, you could start an animation playing and load a file at the same time. Which one completes first is completely system-dependent.

Another example, which isn't really a race condition, but has a similar symptom, is where you use a for..in (or for..each) loop to add multiple listeners for the same event. The listeners will receive the event in the order that they were added, but the for..in loop is random so you'll see random results.

0

精彩评论

暂无评论...
验证码 换一张
取 消