11

Thread 1: is executing this loop

while(running) {
// Do Task()
} 
println("Done");

Thread 2 sets running false In case running is a volatile variable, thread1 comes out of the loop and prints "Done".

My question is if running is not volatile, when does Thread1 reads running variable from the main memory ?

Note: Well i know the happens before relationship about synchronization and volatile variable but the thread 1 does stops even if running is not volatile or synchronized. So my question is when does Thread 1 decides to read from the main memory given that NO SYNCHRONIZATION or and NO VOLATILE

2sb
  • 629
  • 2
  • 12
  • 26

4 Answers4

12

This is described in the JLS under the section Threads and Locks.

When the thread is required to read from main memory is defined in terms of the synchronization order and happens before order. Basically it says that in order for a read to yield the value that was last written, the write needs to happen-before the read.

The happens-before relation is roughly speaking defined in terms of locking/unlocking actions and (with a few exceptions) boils down to the usage of synchronized methods and blocks. Unless you're dealing with volatile variables, the bottom line is usually that you need to synchronize all access to shared data, preferably through AtomicBoolean, a BlockingQueue or some other java.util.concurrent class.

17.4.4 Synchronization Order

Every execution has a synchronization order. A synchronization order is a total order over all of the synchronization actions of an execution. For each thread t, the synchronization order of the synchronization actions (§17.4.2) in t is consistent with the program order (§17.4.3) of t.

Synchronization actions induce the synchronized-with relation on actions, defined as follows:

  • An unlock action on monitor m synchronizes-with all subsequent lock actions on m (where subsequent is defined according to the synchronization order).
  • A write to a volatile variable (§8.3.1.4) v synchronizes-with all subsequent reads of v by any thread (where subsequent is defined according to the synchronization order).
  • An action that starts a thread synchronizes-with the first action in the thread it starts.
  • The write of the default value (zero, false or null) to each variable synchronizes-with the first action in every thread. Although it may seem a little strange to write a default value to a variable before the object containing the variable is allocated, conceptually every object is created at the start of the program with its default initialized values.
  • The final action in a thread T1 synchronizes-with any action in another thread T2 that detects that T1 has terminated. T2 may accomplish this by calling T1.isAlive() or T1.join().
  • If thread T1 interrupts thread T2, the interrupt by T1 synchronizes-with any point where any other thread (including T2) determines that T2 has been interrupted (by having an InterruptedException thrown or by invoking Thread.interrupted or Thread.isInterrupted).

The source of a synchronizes-with edge is called a release, and the destination is called an acquire.

17.4.5 Happens-before Order

Two actions can be ordered by a happens-before relationship. If one action happens-before another, then the first is visible to and ordered before the second.

If we have two actions x and y, we write hb(x, y) to indicate that x happens-before y.

  • If x and y are actions of the same thread and x comes before y in program order, then hb(x, y).
  • There is a happens-before edge from the end of a constructor of an object to the start of a finalizer (§12.6) for that object.
  • If an action x synchronizes-with a following action y, then we also have hb(x, y).
  • If hb(x, y) and hb(y, z), then hb(x, z).

It should be noted that the presence of a happens-before relationship between two actions does not necessarily imply that they have to take place in that order in an implementation. If the reordering produces results consistent with a legal execution, it is not illegal.


Update: If no happens-before relation exists, the thread is never ever required to "refresh its cache". This question and it's accepted answer provides a concrete example of this.

Here is an slightly modified version of the accepted answer:

public class Test {

    static boolean keepRunning = true;

    public static void main(String[] args) throws InterruptedException {

        (new Thread() {
            public void run() {
                while (keepRunning) {
                }
            }
        }).start();

        System.out.println(keepRunning);
        Thread.sleep(1000);
        keepRunning = false;
        System.out.println(keepRunning);

        // main thread ends here, but the while-thread keeps running.
        // (but not if you change the keepRunning to volatile).
    }
}
Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Strictly speaking, that's the correct answer, but giving the (possible) practical implications would be even more useful. – Joachim Sauer Feb 16 '11 at 21:18
  • The happens before order is guaranteed only in case of sync or volatile but that is not my question. My question is when does it happens if the code is not within sync or volatile. – 2sb Feb 16 '11 at 21:42
  • 3
    Ah, then it is not guaranteed to *ever* happen. – aioobe Feb 16 '11 at 21:44
  • @aioobe "The thread is never ever required to refresh" BUT IT DOES. I have tested it running the program on solaris, windows, linux. – 2sb Feb 16 '11 at 21:50
  • Try the program I posted. It keeps running on my machine (ubuntu, java version 1.6) – aioobe Feb 16 '11 at 21:50
  • 1
    Just because it isn't required to refresh doesn't mean that something is wrong if it does. – ide Feb 16 '11 at 21:56
  • 1
    @user59: read what ide wrote. Just because it *does* refresh doesn't mean it's wrong. But depending on the fact that it will eventually refresh is wrong (because the JVM might decide not to refresh it ever). – Joachim Sauer Feb 16 '11 at 22:02
  • @aioobe, just add System.out.println(keepRunning); and see it yourself. Thank you @biziclop for the answer. This is not the main reason for my question. My question is to find out all the influencing parameters. – 2sb Feb 16 '11 at 22:30
1

I know this is too late to add to the well explained answer. But I hope that some one will get help from my answer.The question was about when does java/jvm thread cache refresh happens?.

There is no way jvm will flush or refresh the local thread cache when thread is in runnable state. But in cases like when thread change its state by itself or external thread scheduler, then definitely local thread cache will flush to main memory. Here I just tweaked the example given from the answer

    (new Thread() {
        public void run() {
            while (keepRunning) {
                LockSupport.parkUntil(500);
            }
        }
    }).start();

    System.out.println(keepRunning);
    Thread.sleep(1000);
    keepRunning = false;
    System.out.println(keepRunning);

Now here I just put the current into waiting thread. This method used by many java built in thread pool executor like common thread pool. So here thread will readfrom main memory and stop itself.

sandip
  • 129
  • 5
0

Why not try it yourself?

public class ThreadTest {

private static boolean running = true;

public static void main(String[] args) throws InterruptedException {
    Thread t = new Thread() {
        public void run() {
            while( running ) {
                System.out.println( "Running.");
            }
            long l = System.nanoTime();
            System.out.println( "Stopped at "+l);

        }
    };
    t.start();
    Thread.sleep( 1 );
    running = false;
    long l = System.nanoTime();
    System.out.println( "Stopping at "+l);
}
}

It's by no means a perfect test but it gives you a rough estimate. On my machine the difference was approx 25ms. Of course it has to execute the System.out.println too, but that certainly doesn't take that long.

biziclop
  • 48,926
  • 12
  • 77
  • 104
-1

As thread1 has already read running, it will have a cached value and this will remain valid for some time. This is likely to be us or be ms but could be longer. This is likely to be architecture dependant and also depend on how busy the box is.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • No, it can go on for ever: http://stackoverflow.com/questions/2787094/how-to-demonstrate-java-multithreading-visibility-problems – aioobe Feb 16 '11 at 21:33
  • @Peter. milliSec or micro or nanoSecond, there must be some logic written inside it. That is what i want to know. architecture dependent ? Please elaborate. – 2sb Feb 16 '11 at 21:46
  • @aioobe No it doesn't, you can try yourself. – 2sb Feb 16 '11 at 21:48
  • 2
    @user596048 There's no determistic behavior. It might get the new value immediatly, or it could never get it at all. There's too stuff influencing what could happen to be sure wheter its micro,miliseconds or forever. "Try it" doesn't work - this'll depend on the JVM version, your processor, how the hotspot VM decided to optimize that particluar code any many, many other things. – nos Feb 16 '11 at 21:50
  • @Nos you are near to the answer but i want to know all those logic which is influencing. There should be some logic behind being indeterministic. – 2sb Feb 16 '11 at 21:56
  • From what I have seen, it won't get it immediately, it usually takes about 5 us on my machines, but it can take a few ms. It rarely takes much longer but it can. There is no guarantees. – Peter Lawrey Feb 16 '11 at 21:58
  • 1
    @user596048 "all those logic" probably requires you to read through the whole source code of the jvm and the manual for your processor, and you'd still only get an answer for your particular code. (i.e. did the hotspot compiler decide to compile that particular code to emit a memory read or not, and did it recompile that code 100k iterations later to not perform a memory read ?) – nos Feb 16 '11 at 22:00
  • 1
    Eventually, a thread get context switched out and its cache is invalidated. When the thread starts again, it gets the current value. – Peter Lawrey Feb 16 '11 at 22:00
  • @nos, and the OS code. And it could all change with a patch/update to the kernel. – Peter Lawrey Feb 16 '11 at 22:01
  • 1
    @Peter Lawrey, fact is that if the JIT compiler recognizes that there is no synchronization, it may very well optimize away any future reads altogether. (And from an optimization point of view it *should*!) – aioobe Feb 16 '11 at 22:01
  • 1
    @Peter Lawrey no, not if the jvm decides to cache the value in a register, or on the stack. (and thats from experience, fixing an infinite loop because a variable was not declared volatile) – nos Feb 16 '11 at 22:02
  • @nos Even before that you have to face up to the fact that the Hotspot VM's source code is notoriously poorly documented. – biziclop Feb 16 '11 at 22:13
  • I am liking this discussion, Thank you everyone for participating, lets try to put all the points and then atleast we can give a try to test those points. – 2sb Feb 16 '11 at 22:35
  • It could place the value in a register and possibly might in a future version, but the latest version doesn't do this AFAIK. I have tested Oracle Java 6 update 23 for this exact problem and timed how it runs with optimised code. – Peter Lawrey Feb 16 '11 at 22:35