0

I just need to do an example of race condition with java threads, I writed this code, but I'm not sure if it has a race condition.

Can someone tell me if the below code has a race condition,and also how can I improve it or make it simple?

(sorry for bad english)

public class RaceCondition extends Thread{

static int count=0;   //the variable where the race condition need to happen
static int contador1 = 0;    //variables to count
static int contador2 = 0;


static Thread t1 = new Thread(new Runnable() {
    public void run(){

        while(contador1!=20){
        count ++;
        System.out.print(count + " ");
        contador1++;

        }
    }
     });

static Thread t2 = new Thread(new Runnable() {
    public void run(){
        while(contador2!=20){

        int k = 5;
        count += 5*k;
        System.out.print(count + " ");
        contador2 ++;

        }
    }
     });

public static void main(String[] args) {

     t1.start();
     System.out.println(" ");
     t2.start();

    }

}   
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Bryan Romero
  • 164
  • 2
  • 11
  • 5
    Code has to run to have a race condition. I don't see a declaration for `count`. Does this code compile? – erickson Jan 22 '16 at 20:15
  • 1
    @user If the program ran, threads would continue to run until they completed, because they are not daemon threads. What are you seeing? – erickson Jan 22 '16 at 20:21
  • @user the main thread will die after the main finishes, live threads will not. – John Vint Jan 22 '16 at 20:54

6 Answers6

6

You do have a race condition. Neither of the ++ or += operations is implemented as an atomic operation in Java, the two threads can interfere with each other when they're both trying to read and update the count field.

Also there is no guarantee that updates to the shared variable will be visible across threads so one thread might not even see the other thread's updated value.

You can fix both problems by making the count static variable an AtomicInteger. Use getAndIncrement instead of ++, and getAndAdd instead of +=.

For why ++ works like this, see Why ++ is not implemented as an atomic operation in Java.

Community
  • 1
  • 1
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
0

You do not have any race condition. The threads only access the variables contador1 and contador2 respectively. There is no variable that is modified or read by both threads.

t2 even has its own local k that it uses, rather than the k you marked.

Where is count declared?

Filip Haglund
  • 13,919
  • 13
  • 64
  • 113
0

In order to have a race condition, you need to have threads which read/write a shared portion of memory (for example a global/static variable). Here, your threads don't interact, hence no race condition. Moreover, you redeclar k inside the thread 2 code, so your global k is never used.

Try this instead:

public class RaceCondition extends Thread{

    static int k = 0;       //the variable where the race condition need to happen


    public void run(){

        while( k != 20 ){
            k++;
            try{
                Thread.sleep( 1 );
            }catch( InterruptedException e ){

            }
            System.out.print( k + " " );

        }
    }


    public static void main( String[] args ){

        RaceCondition t1 = new RaceCondition();
        RaceCondition t2 = new RaceCondition();
        t1.start();
        t2.start();
    }

}
Derlin
  • 9,572
  • 2
  • 32
  • 53
0

"A race condition occurs when two or more threads can access shared data and they try to change it at the same time. Because the thread scheduling algorithm can swap between threads at any time, you don't know the order in which the threads will attempt to access the shared data. Therefore, the result of the change in data is dependent on the thread scheduling algorithm, i.e. both threads are "racing" to access/change the data." SOURCE = What is a race condition?

Pay attetion to this part "the result of the change in data is dependent on the thread scheduling algorithm" and how variable count is being manipulated in your code.

Community
  • 1
  • 1
Samuel Paz
  • 235
  • 3
  • 10
0

No. A race condition occurs when two or more threads access the same variable which causes problems in the result. For example,

static int a = 2;
static int b = 0;

static Thread t1 = new Thread(new Runnable() {
    public void run(){
        if(a == 2) {
            b = 5 * a;
            System.out.println(a);
        }
    }
});

static Thread t2 = new Thread(new Runnable() {
    public void run(){
        a = 5;
    }
});

Here, consider just after Thread1 executed if(a == 2), Thread2 changed the value of a = 5. So, instead of getting the expected value 10, we will get 25.

Msp
  • 2,493
  • 2
  • 20
  • 34
-1

this is code :

package RaceCondition;

public class RaceCondition extends Thread {

    static int count = 0;
    static int contador1 = 0;
    static int contador2 = 0;

    static Thread t1 = new Thread(new Runnable() {
        public void run() {

            while (contador1 != 20) {
                count++;
                System.out.print(count + " ");
                contador1++;
            }
            System.out.println("fine t1");

        }
    });

    static Thread t2 = new Thread(new Runnable() {
        public void run() {
            while (contador2 != 20) {
                int k = 5;

                count += 5;

                System.out.print(count + " ");
                contador2++;
            }
            System.out.println("fine t2");
        }
    });

    public static void main(String[] args) {

        t1.start();
        System.out.println(" ");

        t2.start();

    }

}
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Bryan Romero
  • 164
  • 2
  • 11