Note that there are some explanatory texts on larger screens.

plurals
  1. POProvoking a race condition in Java
    primarykey
    data
    text
    <p>I got to write a unit test which provokes a race condition so I can test if I probably fixed the problem later on. The problem is that the race condition only occurs very rarely, maybe because my computer has only two cores.</p> <p>The code is something like the following:</p> <pre><code>class MyDateTime { String getColonTime() { // datetime is some kind of lazy caching variable declared somewhere(does not matter) if (datetime == null) { initDateTime(); //Uses lazy to initlialize variable, takes some time } // Colon time stores hh:mm as string if (datetime.colonTime == null) { StringBuilder sb = new StringBuilder(); //Now do some steps to build the hh:mm string //... //set colon time datetime.colonTime = sb.toString(); } return datetime.colonTime; } } </code></pre> <p>Explanation: initDateTime assigns a new instance to dateTime, therefor, datetime.colonTime is null afterwards (as we want to initialize it lazy, as I stated before). Now if Thread A enters the method and then the scheduler stops it just before it can run initDateTime(). Thread B now runst getColonTime(), sees that datetime is still null and initialzes it. datetime.colonTime is null so the second if block is executed and datetime.colonTime gets the value of the StringBuilder. If then the scheduler stops the thread between this line and the return statement and resumes thread A, the following happens: As A was stopped just before initDateTime is called, A now calls initDateTime(), which will kind of <strong>reset</strong> the datetime object, setting datetime.colonTime to null again. Thread A then will enter the second if block, but the scheduler will interrupt A before datetime.colonTime = sb.toString(); is called. As a conclusion, dateTime.colonTime is still null. Now the scheduler resumes B and the method returns null.</p> <p>I tried to provoke the race condition by having a number of threads calling getColonTime() to a single (final) instance of MyDateTime, but it only fails in some extreeemly rare cases :( Any hints how to write a JUnit "test"?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload