1

I am unable to find this particular scenario answered anywhere.

If I call init() simultaneously from two threads, is one of the calls guaranteed to see that the time is not null anymore? Does time need to be volatile as well?

Does it work like join() which is a synchronisation point?

private Long time;

synchronized void init() {
    if (time != null) {
        throw new IllegalStateException("Already initialised.");
    }

    this.time = System.currentTimeMillis();
}
6
  • I think if the variable is non-volatile there can be problems with caching on multiple processing units. Oct 6, 2014 at 5:35
  • 2
    If this wasn't thread-safe, synchronized would be completely useless.
    – Mat
    Oct 6, 2014 at 5:36
  • Synchronized keyword implicitly implies a Happens before relation ship. So, yes, it is thread safe. Oct 6, 2014 at 5:37
  • Are you saying that all variables touched within a synchronized block are immediately visible to all future synchronized blocks on the same object even if they're not volatile?
    – Monstieur
    Oct 6, 2014 at 5:38
  • @Locutus - yes. Because Synchronization creates a memory barrier which forces data to be fetched from main memory rather than from the cache. So, Whatever data changes in the synchronized block, it is visible once you exit the block. Oct 6, 2014 at 5:40

2 Answers 2

3

When a thread enters this synchronized method, it acquires an implicit lock over the instance, the changes it performs to the state of the instance is visible to the other threads, waiting to enter the synchronized method.

when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.

The last line in the docs mention it clearly. It doesn't matter if the state were volatile or non-volatile.

7
  • What if instead of being a synchronized method, I wrapped the statements in a synchronized(lockObject) block? Now I am changing the state of some other object than the lock object.
    – Monstieur
    Oct 6, 2014 at 5:48
  • Yes, in that case it cannot be guaranteed. Then you could make use of the volatile keyword.
    – BatScream
    Oct 6, 2014 at 5:50
  • 4
    You're pulling a quote out of its context. Having a synchronized block does not guarantee that changes are visible to all threads, only to threads that have subsequently acquired the same monitor using a synchronized block. The quote was only valid in the context of the example class SynchronizedCounter on the page where you pulled your quote from: docs.oracle.com/javase/tutorial/essential/concurrency/… (not you are required to attribute where you take quotes from) Oct 6, 2014 at 5:55
  • It still stands good for Threads sharing the same instance and an instance method being synchronized?
    – BatScream
    Oct 6, 2014 at 5:59
  • @BatScream yes, but only for the threads that have later acquired the synchronized block (and only after they have done so) Oct 6, 2014 at 6:00
0

A simpler solution is use the static class initialiser.

 class TimeHolder {
       public static final long time = System.currentTimeMillis ();
 }

The assumption being that you init either when the class is used the first time or when the object is created.

2
  • It's just an example. I need thread safety for something else.
    – Monstieur
    Oct 6, 2014 at 6:23
  • The more realistic the question, the more appropriate the answer. Oct 6, 2014 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.