Dennis Zandvliet wrote:One more question how do you properly declare js_new and js_old with respect to, scope, synchronization and type, StringBuilder or StringBuffer, and this similar discussion in mind?
First let's talk about type:
1) I 100% disagree with the conclusions the blog post you linked to makes. The Vector class is legacy and should not be used - in ANY situation unless your code must run in a 1.0 JVM (in which case it is the ONLY choice to make). If you need a synchronized version of a List, use the Collections.synchronizedList() or choose one of the better designed Collection implementations in the
java.util.concurrent package (Java 1.5+). The example Producer/Consumer code from the post is a bit contrived and still has synchronization problems when you use Vector (a single call in Vector is synchronized which prevents multiple consumers from attempting to remove the same object or from removing incorrectly indexed object, but iterative calls aren't - to get iterative consistency you need to synchronize the entire loop), and really represents a poor choice of collections (better suited with a Queue, rather than a List, and perhaps a BlockingQueue for what the code is supposed to do.) I guess the conclusion here is that I think the blog post is both irrelevant and incorrect.
2) For js_old and js_new you have 3 choices of type, String, StringBuilder, and StringBuffer. String is immutable - it can't be changed, so it is inherently thread safe. Since js_old is shared between multiple threads I would choose to make it a String. js_new is the results of some data manipulation that builds a CharacterSequence. It is being used only locally to store the results before being assigned to the shared reference. It makes sense to make js_new the same type as js_old, but it doesn't really matter.
Now let's talk scope
1) js_old has to be shared between multiple threads and multiple method calls. This means the scope should be larger than the method - an instance variable, or larger. My personal preference is to store data like this in the ServletContext rather than as instance variables because you have no control over the life-cycle of the Servlet / Servlet Filter, which means that the Filter may be taken out of service and replaced by another (or you could have multiple different instances of the Filter running in different JVMs) without you knowing about it. But that is more of a supposed concern, it isn't real until you get into load balancing / multi-server systems. So my choice for js_old would either be private instance variable or stored in the Servlet Context.
2) js_new is used only in the synchronized block to temporarily hold the value which gets assigned to js_old. As such, it should be created in the smallest scope as possible - inside the synchronized block at least, inside the if statement at best.
3) The method generateJavascript() is really the only code that needs to worry about creating a mutable character sequence. So it should use a StringBuffer or StringBuilder. It should be created local to the method, which means thread safety is not an issue, which lends itself to using a StringBuilder (if you know you are working in a Java 1.5+ environment, or StringBuffer if you don't know the JVM version.)