Saturday, February 24, 2007

Synchronizing static methods

In Java, a lock can be obtained on the instance of a class or on a class itself.

We use lock on a class to synchronize static methods instead of lock on an object. Assume I have a private static data member of a class say staticData which is protected by synchronized static method staticFoo. If two threads t1 and t2 try to enter this critical section - staticFoo, Java language provides support to make sure only one thread is in the critical section. But we cannot prevent thread t3 that operates on instance of that class and modifies private static data member staticData. This means the MT-Safe protection is lost.

The solution to this problem is more of a coding discipline not to access static data members directly in object instances of the class if static data members are protected by synchronized static methods. We cannot really enforce it. This reminds me of a famous statement "Design should be open for extension but closed for modification". This is not the case here. Can you associate this problem to Singleton?

Thursday, February 22, 2007

Scheduling in Java

The classic ways of sharing processor by various threads is Preemptive Scheduling and Time Slicing.

Some designs based on Pipeline work model can be well achieved with Preemptive Scheduling. Designs based on Pool of Workers that execute independent tasks can be well achieved with Time Slicing model. The type of scheduling is also influenced by the support offered by OS for multi-threading.

Java as a language is platform independent and does not allow user to dictate on type of scheduling to use for that particular application. Even though it is possible to specify priorities for a thread, the question of honoring priorities is subject to a particular JVM implementation and the respective OS. We have to believe that the JVM is best designed to give optimal performance for all applications irrespective of their design paradigm.

Wednesday, February 21, 2007

Double Checked Locking for Singleton

This turns out to be an anti-pattern unless used properly with volatile or full method getInstance() synchronization.

http://en.wikipedia.org/wiki/Double-checked_locking

Starting and Stopping Threads...

Usage of Thread.stop(), Thread.resume(), Thread.suspend() and Thread.runFinalizersOnExit() is not encoraged. We should use the following pattern to stop threads. Refer to links:
http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html
http://www.forward.com.au/javaProgramming/HowToStopAThread.html

Saturday, February 10, 2007

Good book on Java Concurrency

Java Concurrency in Practice (Paperback)
by Brian Goetz (Author), Tim Peierls (Author), Joshua Bloch (Author), Joseph Bowbeer (Author), David Holmes (Author), Doug Lea (Author)

Java design patterns - tutorial

Download tutorial on design patterns in Java from James W. cooper :
http://www.ebooksportal.org/2007/01/java-design-patterns-a-tutorial-addison-wesley.ebp