Code to simulate race condition in a Java thread

I am new to Java multithreading. I am studying the concept of the state of race.

Based on Oracle document

http://docs.oracle.com/javase/tutorial/essential/concurrency/interfere.html

I created a code sample similar to below

public class CounterTest {

    public static void main(String[] args) {

        Thread thread1 = new Thread(new CounterIncThread());
        thread1.setName("add thread");
        thread1.start();

        Thread thread2 = new Thread(new CounterDecThread());
        thread2.setName("sub thread");
        thread2.start();

        Thread thread3 = new Thread(new CounterIncThread());
        thread3.setName("add thread2");
        thread3.start();
    }

}


class CounterIncThread implements Runnable {
    public void run() {
        SynchronizedCounter counter = new SynchronizedCounter();
        counter.increment();
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String threadName =
                Thread.currentThread().getName();
        System.out.println(threadName+ ": "+counter.value());
    }
}

class CounterDecThread implements Runnable {
    public void run() {
        SynchronizedCounter counter = new SynchronizedCounter();
        counter.decrement();
        String threadName =
                Thread.currentThread().getName();
        System.out.println(threadName+ ": "+counter.value());
    }
}

class SynchronizedCounter {
    private int c = 0;

    public  void increment() {
        c++;
    }

    public   void decrement() {
        c--;
    }

    public  int value() {
        return c;
    }

}

There are no racing conditions in the code. Could you help me how to stimulate the race using the code above?

thank

+4
source share
4 answers

, , ( ) ( ). , , volatile .

, , blog.

A B , - . , . 18, 19. , 19, B, , , A . , . .

enter image description here

, , , .

, , , , , , , . , , .

. :

  • ,
  • MyCounter
  • , 1,000,000
  • join(), , Thread.sleep,
  • c MyCounter ; , JVM , . , , :)
  • , , 2 000 000. - , .

.

public class CounterTest {    
    public static void main(String[] args) throws InterruptedException {   
        MyCounter counter = new MyCounter();

        Thread thread1 = new Thread(new CounterIncRunnable(counter));
        thread1.setName("add thread");
        thread1.start();

        Thread thread2 = new Thread(new CounterIncRunnable(counter));
        thread2.setName("add thread2");
        thread2.start();

        thread1.join();
        thread2.join();

        System.out.println(counter.value());
    }    
}


class CounterIncRunnable implements Runnable {
    private MyCounter counter;

    public CounterIncRunnable(MyCounter counter) {
        this.counter = counter;
    }

    public void run() {
        for ( int i=0; i<1000000; i++ ) {
            counter.increment();
        }
    }
}


class MyCounter {
    private volatile int c = 0;

    public  void increment() {
        c++;
    }

    public   void decrement() {
        c--;
    }

    public  int value() {
        return c;
    }    
}

, ; MyCounter, . , ​​ 2000000. , . , c .

+5

- , ,

  read a value
  think for a bit, giving another thread a chance to get in
  increment the value and write it back

, , , 43, ,

  A reads value 43
  A thinks
  A increments and writes 44
  B reads value 44
  B thinks
  B increments and writes 45

- " "

  A reads value 43
  A thinks
  B reads value (it still) 43
  B thinks
  B increments 43 to 44 and writes
  A increments 43 to 44 and write
  // the value is now 44, and we expected it to be 45

, , , , , , , "" .

:

1). ,

2). , . , , " ", , . , , , , , JVM .

+5

, . SynchronizedCounter (, ). counter runnable.

CounterIncThread(SynchronizedCounter counter)
{
   this->counter = counter;
}

CounterDecThread(SynchronizedCounter counter)
{
   this->counter = counter;
}

...
SynchronizedCounter counter = new SynchronizedCounter();
Thread thread1 = new Thread(new CounterIncThread(counter));
Thread thread2 = new Thread(new CounterDecThread(counter));
Thread thread3 = new Thread(new CounterIncThread(counter));

. runnable. . , .

for(int i = 0; i < 100000; i++) <-- 100000 is just on the top of my head
{
    counter.increment(); 
}

, , , 100000 * 2.

, . ,

+1

If the fingerprints are displayed "correctly", this is due to the fact that there are no threads. Try creating 100 threads and you will see that prints are not displayed sorted. If cthe SynchronizedCounter is static, you can see the race status, as the threads read the same variable.

0
source

All Articles