About Singleton, Threads and Flex
June 12th, 2008 at 10:19 am by Michel KlabbersWhile I was preparing my Flex presentation (Logica Arnhem, May 21st) I searched Google for Flex related sites and posts. The Singleton pattern appeared numerous times. In the Java world the Singleton pattern [1] is still subject of huge discussion.
Sometimes it even ends up as number one on a hate list [2].
Let’s take a look at some example code:
public class Model {
private static Model _model;
...
public static Model getInstance() {
if(_model == null) {
_model = new Model();
}
return _model;
}
private Model() {
...
}
}
One thing you’ll immediately notice missing is the synchronized statement to make the initialization safe. Now let’s take a look at a similar Actionscript 3 code example in Flex:
package mvc {
public class Model {
private static var _model:Model;
[Bindable]
public var data:ArrayCollection =
new ArrayCollection([ "Cool movie", "Really cool movie" ]);
public static function getInstance() : Model {
if(_model == null) {
_model = new Model();
}
return _model;
}
// Constructor: should be private/protected (i.e. Singleton class)
// but Flex compiler can't cope
public function Model() {
trace("Do not call this function directly!");
}
}
}
If you’re not accustomed to Flex the Bindable metadata tag [3] might seem a bit awkward. The Bindable tag makes sure the data is, when altered, automatically updated in your view. Instead of an array or collection type an ArrayCollection is used. The ArrayCollection is versatile and can also be used for mappings.
Again the synchronized statement is missing. But is it really necessary? To be able to answer this question we have to dive deeper into Flex.
Flex applications are, like Flash applications, compiled into an SWF file. Once a user visits the webpage containing your Flex application, the SWF file is downloaded to and run from the client computer. Instead of a seperate session each user receives their own copy of your Flex application. The client computer runs the Flash VM, which in turn fires up the local copy of your Flex application.
Furthermore, Flex uses the Actionscript scripting language. The current version is Actionscript 3. Actionscript 3 is single-threaded. By now you probably already see where this is going. The single-threaded nature of Flex applications means synchronization is not required.
For Flex we have to seperate programming threading support and VM threading support. Whereas Actionscript 3 will not support user threads in near future, I predict Adobe will soon add the internal useage of threads by the Flash VM for responsiveness and speed. Currently a cpu-intensive background task can prevent any user-interaction and application-response in a Flex application. Using additional VM threads is a powerful technique to solve this issue and increase user responsiveness.
Adobe promises to incorporate GPU Hardware support in Flash Player 10. That will be the first step to free cpu resources for important background tasks and improve responsiveness. The official statement about Flash Player 10 Beta from Adobe is as follows:
“With Flash Player 10 beta, developers can enable SWF content to render through the memory bandwidth and computational horsepower of the GPU hardware processor,
freeing up the CPU to do more - such as render 3D content and intricate effects, and process complex business logic.”
Links:
Popularity: 1594 points


June 13th, 2008 at 7:26 pm
First, you don’t need to synchronize the getInstance() method in the Java version. Instead, simplify the code to something more elegant. For example:
public class Singleton{
private final static Singleton instance = new Singleton();
private Singleton(){}
public static Singleton getInstance(){
return instance;
}
}
Now you don’t need to worry about synchronizing your getInstance() method.
June 14th, 2008 at 3:15 pm
I disagree. If they make the VM multithreaded then it will change the currently accepted semantics of actionscript - potentially breaking any existing AS3 code. Surely this would cause too much disruption?
Or is there some other way they can introduce native threads while maintaining backwards compatibility? What are Mozilla up to w/Tamarin?