With the release of the BlackBerry PlayBook next week, a good amount of time has been spent coding in ActionScript rather then Java. One of the main differences between ActionScript and most other object oriented languages is that ActionScript does not support threads.
Instead ActionScript is an event driven language. Instead of worrying about a main thread, or which one of many threads are running you instead just respond to events as they happen. Languages like Java are like this in some respects, such as reacting to key presses, but not to the point where you get rid of threads. As such in ActionScript methods doing complex work are never blocking, and you don’t have to create temporary threads for doing things like editing the main UI. Furthermore using the flash.utils.Timer class is a much cleaner way to periodically preform an action then any separate thread and loop system that you would otherwise have to do.
While the lack of threads may be limiting for the few uses that actually need them, for the most part ActionScript instead gives you an alternative for the instances where you were only using threads for lack of a more efficient option. The end result is that ActionScript can result in cleaner and more efficient code that is better for both the developer and the user.
As a veteran software developer and recent (obviously) PlayBook developer, I’ve enjoyed some of your observations, so please take the criticism that follows in stride.
Talking about PlayBook development, “ActionScript (Life without threads)” is certainly apropos. I programmed in ActionScript for Flex 1.0 (one of The Few, I suppose) and Flex 2.0. Developing now for the PlayBook, I often found myself wishing that I could employ a logically consistent, efficient and – in regard to both syntax and semantics – non-abortion-on-toast, non-repeatedly-papered-over programming language and, certainly, a programming language expressive enough to provide multi-threading functionality. Say Java or C#, to reference a couple of mainstream stalwarts. (BTW, does anyone seriously doubt that we shall, before too long, see multithreading added to Flash programming platforms? It’ll be added, alas, as one glues on another layer of mache.)
One should not conflate the (welcome) presence of Events with the unfortunate (indeed untenable, just as concurrent programming, on multicore processors enters a golden age) absence of multi-threading.
Further, some of your statements in the blog entry are simply false. Do a quick google, for example, and you’ll find any number of examples of the Flash/Flex UI brought to complete halt by required “business logic” computation.
Flash/ActionScript/Flex
– resorts to providing special-case kludges,
arbitrarily providing implicit background threading
just for particular tasks, such as gfx filtering
and several varieties of RPC.
– makes the programmer jump through hoops
to role his own solutions to try to implement
reasonable facsimilies of design patterns taken
for granted in real programming systems.
– I could go on…
And what of the noble Timer class you favour?
It’s wonderful, if your idea of superior involves concepts such as “non-deterministic” (while, ironically, the very concept “Timer” stands for determinism) and “low-resolution”.
Citing the Timer class as representing a “more efficient” and “cleaner” option than the by-contrast complete, well-designed, performant and correct threading semantics of Java, C#, et al., is absurd. Good grief, the Timer is second-order inadequate: it depends upon frame rate that depends upon the content rendered.
I suppose I’ve just sketched out some of what I’d like to say on this topic, but I’ll have to leave it at that for the time being.
I agree with Will. Unfortunately, the Timer class does not appear to launch a separate thread but instead simply preempts the execution of the main thread. I found this out while I was toying around with some code for genetic algorithms in AS3 and the UI locked up whenever the timer would fire. Not only that, but if you are trying to run code that requires more calculations than what you allotted for your timer but at the same time requires prior results, you are pretty much screwed. The only reason I’m sticking with flash is that I’m developing simple games that I sell to flash sites. Otherwise, I’d probably dump it for the unity framework.