Several Groovy threads workin on one table

Scripting
Pete
2015-12-30
2015-12-31
  • Pete

    Pete - 2015-12-30

    Hi.

    So far this thing has very little to do 3D but anyway, I was testing multithreading in AoI-Groovy. Here is my test script.

    /* A threading experiment in Groovy     */
    /* Several threads working on one table */
    
    int nt = 10; // number of threads
    
    // A method to be called by the threads
    int NewA()
    {
        println ("New A");
        return 3;
    }
    
    tx = Thread.start{ // main therad
    
        Thread[] t = new Thread[nt];   // Array for the threads
        int[] a = new int[nt];         // The table to work on
        a[8] = 123;                    // A test case...
    
        for(int i=0;i<nt;i++) // sub therads
        {
            t[i] = Thread.start
            {
                a[i] = NewA();
                println(i + " " + a[i]);
            }
            //t[i].sleep(10);
        }
    
        for(int j=0;j<nt;j++)
        {
            t[j].join();
            println("Join " + j);
        }
    
        // Printing out the table
        // tx.sleep(1000);
        println();
        for(int k=0;k<nt;k++)
        {
            println("a" + k + " = " + a[k]);
        }
        println();
    }
    

    The for loop with t[j].join() is supposed to make sure that all the sub-threads are finished running before the main thread proceeds, right?

    What happens is, that when I keep hitting the Execute button, I get a different result every time. Sometimes all of the a[]:s have a new value. Some times none of them. Most commonly some of them.

    What seems to help is to uncomment the t[i].sleep(10) but that naturally slows the script down, which consumes the beneft of the multithreading and it is not fool proof either -- the wait should be even longer.

    What is going on and is there a simpe (as mechanical as possible ;) ) solution to this? The groovy examples that I have managed to find seem to contain a lot of Groovy specific vocabulary, that I am not familiar with and I probably don't even know what to look for....

    BR

    Pete

     
  • Peter Eastman

    Peter Eastman - 2015-12-30

    The problem is that the thread body has a reference to the variable i, which is defined in the outer context. But that variable keeps changing. So by the time a thread executes, the value of i may be different from what it was when you created the thread.

    The following fixes it by creating a new variable to hold the index of each thread:

        for(int i=0;i<nt;i++) // sub therads
        {
            int index = i;
            t[index] = Thread.start
            {
                a[index] = NewA();
                println(index + " " + a[index]);
            }
            //t[i].sleep(10);
        }
    

    Peter

     
  • Pete

    Pete - 2015-12-31

    Of course! Thanks.

    For a moment I suspected it'd be something like that. (Hence the j and k in the other for loops.) The exampe that I followed was not using the loop variable in the process, so I missed it. :)

    -P-

     
    Last edit: Pete 2015-12-31

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks