Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Race condition: Min and Max range of an integer

I was recently asked this question in an interview.

Given the following code, what will be the min and max possible value of the static integer num?

import java.util.ArrayList; import java.util.List;  public class ThreadTest {     private static int num = 0;      public static void foo() {         for (int i = 0; i < 5; i++) {             num++;         }     }      public static void main(String[] args) throws Exception{         List<Thread> threads = new ArrayList<Thread>();         for (int i = 0; i < 5; i++) {             Thread thread = new Thread(new Task());             threads.add(thread);             thread.start();         }         for (int i = 0; i < 5; i++) {             threads.get(i).join();         }         // What will be the range of num ???         System.out.println(ThreadTest.num);     } }  class Task implements Runnable {     @Override     public void run() {         ThreadTest.foo();     }  } 

I told them that the max value would be 25 (in case there is no race condition), and min would be 5 (in case of a race condition between all the threads at every iteration).
But the interviewer said the the min value can go even below 5.
How is that possible?

like image 925
Anmol Singh Jaggi Avatar asked Sep 29 '19 10:09

Anmol Singh Jaggi


People also ask

What are race conditions in C?

What are race conditions? Race conditions in software or any system occur when the desired output requires that certain events occur in a specific order but that the events don't always happen in that order. There is a 'race' between the events and if the wrong events win, the program fails.

What are the types of race condition?

Static, dynamic, and essential forms A static race condition occurs when a signal and its complement are combined. A dynamic race condition occurs when it results in multiple transitions when only one is intended. They are due to interaction between gates.

What is race condition explain?

A race condition is an undesirable situation that occurs when a device or system attempts to perform two or more operations at the same time, but because of the nature of the device or system, the operations must be done in the proper sequence to be done correctly.

What is race condition in Java with example?

ADVERTISEMENT. In layman terms, a race condition can be defined as, a condition in which two or more threads compete together to get certain shared resources. For example, if thread A is reading data from the linked list and another thread B is trying to delete the same data.


1 Answers

I claim the minimum value possible is 2.

The key to this is the non-atomicity of num++, i.e., it is a read and a write, which may have other operations in between.

Call the threads T1..T5:

  • T1 reads 0, T2 reads 0;
  • T1 writes 1, and then reads and writes 3 times.
  • Then T2 writes 1;
  • Then T1 reads 1;
  • Then T2-5 do all of their work
  • Then, finally, T1 writes 2.

(Note: the result 2 is not dependent either on the number of threads, or the number of iterations, provided there are at least 2 of each.)

But the honest answer to this is: it really doesn't matter. There is a data race, as defined in JLS 17.4.5:

When a program contains two conflicting accesses (§17.4.1 ["Two accesses to (reads of or writes to) the same variable are said to be conflicting if at least one of the accesses is a write."]) that are not ordered by a happens-before relationship, it is said to contain a data race.

(There is an absence of happens-before relationships between the actions in the threads)

So you can't usefully rely on whatever it does. It is simply incorrect code.

(Moreover, I know the answer to this not because of some hard-won battle debugging multithreaded code, or deep technical reading: I know this because I have read this answer before elsewhere. It's a parlour trick, nothing more, and so asking the minimum value isn't a very good interview question).

like image 98
Andy Turner Avatar answered Oct 04 '22 17:10

Andy Turner