[concurrency-interest] Class variables and concurrency

Luís Matta levmatta at uol.com.br
Fri Nov 4 20:02:28 EST 2005


Hello all,
    today I found a very poor code in a webapp that I am helping to develop:
(Obvious buggy)
class Foo {
    public static String CONST_SQL;
    public static String CONST_SQL1 = "select * from a where b = ";
    public static String getA(String param) {
       CONST_SQL = CONST_SQL1 + param;
        ...run query and return result
    }
}
(corrected to)
class Foo {
    //speed is the priority not code beauty
    public static String CONST_SQL1 = "select * from a where b = ";
    //reentrant code
    public static String getA(String param) {
       String CONST_SQL = CONST_SQL1 + param;
        ...run query and return result
    }
}
    Since this method can be run by multiple threads, and we are using 
class variables, this is a theoretical bug. But is it a actual one, is 
there a pratical situation where this bug will manifest itself?
    I ask because since the variable is not synchronized, it appears to 
me that it will get cached and will never be refreshed in any pratical 
situation.

What are the rules for concurrent access to class variables, and what 
how would the Sun JVM (version 1.4.3) behave?

Thanks,
Luís

PS: This will run on a powerfull machine (I just know it is new and runs 
solaris) ontop of websphere.
PS2: I know that caching is system dependent and not especified, but I 
am interest in the pratical side of things



More information about the Concurrency-interest mailing list