Showing posts with label Java Myths. Show all posts
Showing posts with label Java Myths. Show all posts

Wednesday, August 22, 2007

Does Synchronization helps in Managing volatility?

Very often the developers tries to exploit the term Synchronization.
Though Volatile is not a replacement to Synchronization, its just solves the common problems for which the developers tend to use Synchronization. It does nothing more than but just avoids the Compiler to develop the caches for the Volatile Objects, so that the programme can read the Latest value and not the Stale values of an Object.
Synchronization should be used only when the system wants only 1 Thread to execute a particular set of Lines for example Singleton Pattern. Keep in Mind it has performance overheads.
But Synchronization in itself is not a very efficient way. It is being ruled by the key of "All or Nothing" for example in the Synchronized collections like java.util.Hashtable has all the methods as Synchronized even getXXX() methods are also rendered as Thread safe, though it should not be.

Read More!

Is Singleton Pattern really a Singleton?

It’s a lie! (in most Java systems) - Singletons in Java are based on static variables, which are held per-classloader, not per-VM. In most systems of any complexity these days (any based on an app server, OSGi, Eclipse, plugins, etc) many classloaders will be involved. In that case, it is quite easy for two plugins to create their own instance of a singleton. Sometimes this is done by design and is desirable. But it’s also easy to screw up. It’s also usually critical that the singleton get created in the right classloader, which can make lazily constructing the singleton tricky.

Read More!