Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I find that the examples used to explain synchronization and volatility are contrived and difficult to understand the purpose of. Here are my preferred examples:</p> <p><strong>Synchronized:</strong></p> <pre><code>private Value value; public void setValue(Value v) { value = v; } public void doSomething() { if(value != null) { doFirstThing(); int val = value.getInt(); // Will throw NullPointerException if another // thread calls setValue(null); doSecondThing(val); } } </code></pre> <p>The above code is perfectly correct if run in a single-threaded environment. However with even 2 threads there is the possibility that <code>value</code> will be changed in between the check and when it is used. This is because the method <code>doSomething()</code> is <em>not atomic</em>.</p> <p>To address this, use synchronization:</p> <pre><code>private Value value; private Object lock = new Object(); public void setValue(Value v) { synchronized(lock) { value = v; } } public void doSomething() { synchronized(lock) { // Prevents setValue being called by another thread. if(value != null) { doFirstThing(); int val = value.getInt(); // Cannot throw NullPointerException. doSecondThing(val); } } } </code></pre> <p><strong>Volatile:</strong></p> <pre><code>private boolean running = true; // Called by Thread 1. public void run() { while(running) { doSomething(); } } // Called by Thread 2. public void stop() { running = false; } </code></pre> <p>To explain this requires knowledge of the Java Memory Model. It is worth reading about in depth, but the short version for this example is that Threads have their own copies of variables which are only sync'd to main memory on a synchronized block and when a volatile variable is reached. The Java compiler (specifically the JIT) is allowed to optimise the code into this:</p> <pre><code>public void run() { while(true) { // Will never end doSomething(); } } </code></pre> <p>To prevent this optimisation you can set a variable to be volatile, which forces the thread to access main memory every time it reads the variable. Note that this is unnecessary if you are using synchronized statements as both keywords cause a sync to main memory.</p> <p>I haven't addressed your questions directly as Francis did so. I hope these examples can give you an idea of the concepts in a better way than the examples you saw in the Oracle tutorial.</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. 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