Flex Threading /Asynchronus Behavior
Is Flex both single-threaded and asynchronus? If so, how can a programming model behave in both the ways? Please explain me with an instance, am getting 开发者_高级运维cornered with this.
It'll be more correct to say Flash instead Flex. Flex is just framework and such fundamental concepts as threading and asynchrony are related with technology, i.e. Flash.
Yes, Flash is single-threaded: you cannot create threads on your own. But there are some moments, where flash provides you asynchrony. For example HttpService, WebService, URLLoader classes
What does it mean? It means, that from the moment, when you send your request to the moment, when you receive response, you can continue other code execution and updating displaylist. And when you get responce, events are dispatched (or when request fails).
Also in AIR some operations with files can be processed in an asynchronous way (copyToAsync, deleteFileAsync, getDirectoryListingAsync,...). The same here: while your file is copying, for example, you receive progress event and you can update views (progress bar for example).
Note that you can use multi-threading with Flash by using an external Alchemy module (a C program) or PixelBender (image processing). By the way, Adobe is currently working on the possibility to launch several SWFs on different threads.
And considering the difference between multi-threading and asynchronous behavior, just remember that Flash is "frame-based". Every code you write, even when listening to an asynchronous event, will be executed in a specific and linear flow on each frame loop.
Flex/ActionScript is single-threaded by Flash isn't. Some operations, like URLLoader create another thread you cannot control. Once it finishes, you get an event from the URLLoader object.
Short answer, the code interpreted by the Flash Player is single threaded, but the Flash Player itself is multi-threaded, hence why you can have asynchronous events/functions.
As mentioned before, actual process threading is handled on the player level, which does not currently support true multi-threading.
However, that is not to say you can't have a programming model that behaves in an asynchronous way. It's pretty safe to say that the purpose of any flash application is to drive a display in one way or another, so any processing strategy is really driven by the display list.
If one is talking about an asynchronous programming model in Flex, they are most likely referring to the invalidation/update process that takes place in the Flex component life cycle. This creates a way for expensive processes to be run on the next display list update, creating a semi-asynchronous programming model.
This kind of faux multi-threading is also important for applications that require a lot of iterations over large sets of data, such a complex data visualization. A large algorithm can be defined as a series of smaller chunks, each one being processed on a display list update (frame change). This allows the rest of the application to process events and make any display updates before processing the next chunk.
精彩评论