Singularity
Singularity

Reputation: 89

Java Threads (Race Condition)

I have doubt in the following piece of code. In this i expect race condition to occur for both the static variables 'a' and 'b' and the expect the output for both the variables to be less than 1000. However, the expected behaviour is not observed when i execute the below code as shown i.e. the output for b is always 1000. But, when i uncomment the lines marked with arrows and execute the below code, the race condition is observed for both the variables i.e the output for both the variables 'a' and 'b' is less than 1000. Need help with the same. Pardon me if i have missed out or ignored any of the basic or elementary concepts of threading considering the fact that i am still a newbie to java threads !!!

public class SampleRace {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SampleRace1 a = new SampleRace1();
        SampleRace1 b = new SampleRace1();
        Thread t1 = new Thread(a);
        Thread t2 = new Thread(b);
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //System.out.println(SamapleLoop.a); // <---------------------
        System.out.println(SampleRace1.b);
    }

}

class SamapleLoop {
    public static int a = 0;
    public static void loop() {
        a++;
    }
}

class SampleRace1 implements Runnable {
    public static int b = 0; 
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for (int i = 1; i <= 500; i++) {
            //SamapleLoop.loop(); // <------------------------
            b++;
        }
    }

}

Upvotes: 1

Views: 297

Answers (2)

Emir Arditi
Emir Arditi

Reputation: 107

If you want I can suggest a more proper race condition you can work on

import java.util.concurrent.atomic.AtomicInteger;

/*
 * author: Atom Karinca
 */

/*
 * Run this program multiple times to see the output.
 * How does the result vary? Why does that happen?
 *
 */
class Racer extends Thread {    
    private static final int NUM_ITERS = 1000000;

    public void run() {
        for(int j = 0; j < NUM_ITERS; j++) {
            RaceCondition.sum();
        }
    }    
}

public class RaceCondition {
    public static long sum = 0;


    // What happens when you make this method "synchronized"?
    // Do you still see the bug?    
    //synchronized 
    public static void sum() {
        sum = sum + 1;
    }

    public static void main(String[] args) throws InterruptedException {        
        Racer leftAdder = new Racer();
        Racer rightAdder = new Racer();

        leftAdder.start();
        rightAdder.start();

        // wait for the threads to finish
        leftAdder.join();
        rightAdder.join();

        System.out.println("Sum: " + sum);
    }
}

Upvotes: 0

dcastro
dcastro

Reputation: 68640

Try increasing 500 to a 500.000.000, or doing more complex operations inside the loop. There's a chance that thread a is running to completion before thread b is even started - so they end up running in sequence, rather than in parallel.

You can also call Thread.yield every few cycles to give up your timeslice and let the other thread run.

Upvotes: 1

Related Questions