[concurrency-interest] Class variables and concurrency

Luís Matta levmatta at uol.com.br
Sat Nov 5 17:05:31 EST 2005

Thanks Dawid Kurzyniec,
    just to clarify some things (and ask another)...

When I sad "theoretical bug" , and I realy missed on the words there, what I meant was: it is a coding bug, but will is it one that will manifest itself (actual bug). Sorry.

OK, so I got of my lazy butt and I have created a simple test attached below and the bug _*DID*_ manifest itself.
Any comments on it?.

Results on a machine running Windows XP Sp2 on a P4 with HT (aproximate maximuns):
jvm  runs threads iterations result
5_05 5    50      |10000      0.008%
6b54 5    50      10000      0.05%
5-05 3    2       100        2.5%

I believe that 2 threads is my systems maximum concurrent capability, so I tested it.
Note that the jvm 6b54 gives a much higher result. How come?|

Luís Matta

|import java.util.concurrent.atomic.AtomicInteger;
public class Main {
    private static String CONST;
    private static final String CONST1 = "1+";
    protected static AtomicInteger atomic = new AtomicInteger(0);
    public Main() {
    public static String doIt(char param) {
         CONST =  CONST1  + param;
         return CONST;
    public static void main(String[] args) {
        char [] params = new char[50];//number of runs
        for (int i=0; i<params.length; i++){
            params[i] = (char) i;
        for (char param: params) {
            Thread t = new Thread(new ThreadTest(param));
    private static class ThreadTest implements Runnable {
        public ThreadTest(char param) {
            this.param = param;
        protected char param;

        public void run() {
            for (int i =0; i<10000; i++){//number of iterations
                String res = Main.doIt(param);
                if (res.charAt(2)!=param){
            System.out.println("Atomic value:"+Integer.toString(atomic.get()));
PS: Thanks for the "final" modifier I did miss it in the code. Much better now.
PS: When I mentioned speed, I was refering to coding one. The app MUST be released by the end of the month.||

More information about the Concurrency-interest mailing list