Note that there are some explanatory texts on larger screens.

plurals
  1. POThread concurrency issue even within one single command?
    primarykey
    data
    text
    <p>I am slightly surprised by what I get if I compile and run the following (horrible non-synchronized) Java SE program.</p> <pre><code>public class ThreadRace { // this is the main class. public static void main(String[] args) { TestRunnable tr=new TestRunnable(); // tr is a Runnable. Thread one=new Thread(tr,"thread_one"); Thread two=new Thread(tr,"thread_two"); one.start(); two.start(); // starting two threads both with associated object tr. } } class TestRunnable implements Runnable { int counter=0; // Both threads can see this counter. public void run() { for(int x=0;x&lt;1000;x++) { counter++; } // We can't get here until we've added one to counter 1000 times. // Can we?? System.out.println("This is thread "+ Thread.currentThread().getName()+" and the counter is "+counter); } } </code></pre> <p>If I run "java ThreadRace" at the command line, then here is my interpretation of what happens. Two new threads are created and started. The threads have the same <code>Runnable</code> object instance <code>tr</code>, and so they see the same <code>tr.counter</code> . Both new threads add one to this counter 1000 times, and then print the value of the counter.</p> <p>If I run this lots and lots of times, then usually I get output of the form</p> <pre><code>This is thread thread_one and the counter is 1000 This is thread thread_two and the counter is 2000 </code></pre> <p>and occasionally I get output of the form</p> <pre><code>This is thread thread_one and the counter is 1204 This is thread thread_two and the counter is 2000 </code></pre> <p>Note that what happened in this latter case was that thread_one finished adding one to the counter 1000 times, but thread_two had started adding one already, before thread_one printed out the value of the counter. In particular, this output is still comprehensible to me.</p> <p>However, very occasionally I get something like</p> <pre><code>This is thread thread_one and the counter is 1723 This is thread thread_two and the counter is 1723 </code></pre> <p>As far as I can see, this "cannot happen". The only way the <code>System.out.println()</code> line can be reached in either thread, is if the thread has finished counting to 1000. So I am not bothered if <em>one</em> of the threads reports the counter as being some random number between 1000 and 2000, but I cannot see how <em>both</em> threads can get as far as their <code>System.out.println()</code> line (implying both for loops have finished, surely?) and counter not be 2000 by the time the second statement is printed.</p> <p>Is what is happening that both threads somehow attempt to do <code>counter++</code> at exactly the same time, and one overwrites the other? That is, a thread can even be interrupted even in the <em>middle</em> of executing a <em>single statement</em>?</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