Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Because static variables are so widely shared they are extremely un-thread safe.</p> <p>Consider what happens if two threads call your getInstance at the same time. Both threads will be looking at the shared static <code>obj</code> and both threads will see that <code>obj</code> is null in the if check. Both threads will then create a new <code>obj</code>.</p> <p>You may think: "hey, it is thread safe since obj will only ever have one value, even if it is initialized multiple times." There are several problems with that statement. In our previous example, the callers of getInstance will both get their own <code>obj</code> back. If both callers keep their references to <code>obj</code> then you will have multiple instances of your singleton being used.</p> <p>Even if the callers in our previous example just did: <code>MyClass.getInstance();</code> and didn't save a reference to what <code>MyClass.getInstance();</code> returned, you can still end up getting different instances back from getInstance on those threads. You can even get into the condition where new instances of <code>obj</code> are created even when the calls to getInstance do not happen concurrently!</p> <p>I know my last claim seems counter-intuitive since the last assignment to <code>obj</code> would seem to be the only value that could be returned from future calls to <code>MyClass.getInstance()</code>. You need to remember, however, that each thread in the JVM has its own local cache of main memory. If two threads call getInstance, their local caches could have different values assigned to <code>obj</code> and future calls to getInstance from those threads will return what is in their caches.</p> <p>The simplest way to make sure that getInstance thread safe would be to make the method synchronized. This will ensure that</p> <ol> <li>Two threads can not enter getInstance at the same time</li> <li>Threads trying to use <code>obj</code> will never get a stale value of <code>obj</code> from their cache</li> </ol> <p><strong>Don't</strong> try to get clever and use double checked locking: <a href="http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html" rel="noreferrer">http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html</a></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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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