天天看點

JIT與可見性

many developers of multi-threaded code are familiar with the idea that different threads can have a different view of a value they are holding, this not the only reason a thread might not see a change if it is not made thread safe.  the jit itself can play a part.

when you have multiple threads, they will attempt to minimise how much they will interact e.g. by trying to access the same memory.  to do this they have a separate local copy e.g. in level 1 cache.  this cache is usually eventually consistent.  i have seen short periods of between one micro-second and up to 10 milli-seconds where two threads see different values.  eventually the thread is context switched, the cache cleared or updated.  there is no guarantee as to when this will happen but it is almost always much less than a second.

the java memory model says there is no guarantee that a field which is not thread safe will ever see an update.  this allows the jit to make an optimisation where a value only read and not written to is effectively inlined into the code.  this means that even if the cache is updated, the change might not be reflected in the code.

this code will run until a boolean is set to false.

<code>01</code>

<code>&lt;br /&gt;</code>

<code>02</code>

<code>static</code> <code>class</code> <code>mytask</code><code>implements</code> <code>runnable {&lt;br /&gt;</code>

<code>03</code>

<code>    </code><code>private</code> <code>final</code> <code>int</code> <code>looptimes;&lt;br /&gt;</code>

<code>04</code>

<code>    </code><code>private</code> <code>boolean</code> <code>running =</code><code>true</code><code>;&lt;br /&gt;</code>

<code>05</code>

<code>    </code><code>boolean</code> <code>stopped =</code><code>false</code><code>;&lt;/p&gt;</code>

<code>06</code>

<code>&lt;p&gt;   </code><code>public</code> <code>mytask(</code><code>int</code> <code>looptimes) {&lt;br /&gt;</code>

<code>07</code>

<code>        </code><code>this</code><code>.looptimes = looptimes;&lt;br /&gt;</code>

<code>08</code>

<code>    </code><code>}&lt;/p&gt;</code>

<code>09</code>

<code>&lt;p&gt;   </code><code>@override</code><code>&lt;br /&gt;</code>

<code>10</code>

<code>    </code><code>public</code> <code>void</code> <code>run() {&lt;br /&gt;</code>

<code>11</code>

<code>        </code><code>try</code> <code>{&lt;br /&gt;</code>

<code>12</code>

<code>            </code><code>while</code> <code>(running) {&lt;br /&gt;</code>

<code>13</code>

<code>                </code><code>longcalculation();&lt;br /&gt;</code>

<code>14</code>

<code>            </code><code>}&lt;br /&gt;</code>

<code>15</code>

<code>        </code><code>}</code><code>finally</code> <code>{&lt;br /&gt;</code>

<code>16</code>

<code>            </code><code>stopped =</code><code>true</code><code>;&lt;br /&gt;</code>

<code>17</code>

<code>        </code><code>}&lt;br /&gt;</code>

<code>18</code>

<code>19</code>

<code>&lt;p&gt;   </code><code>private</code> <code>void</code> <code>longcalculation() {&lt;br /&gt;</code>

<code>20</code>

<code>        </code><code>for</code> <code>(</code><code>int</code> <code>i =</code><code>1</code><code>; i &amp;lt; looptimes; i++)&lt;br /&gt;</code>

<code>21</code>

<code>            </code><code>if</code> <code>(math.log10(i) &amp;lt;</code><code>0</code><code>)&lt;br /&gt;</code>

<code>22</code>

<code>                </code><code>throw</code> <code>new</code> <code>assertionerror();&lt;br /&gt;</code>

<code>23</code>

<code>    </code><code>}&lt;br /&gt;</code>

<code>24</code>

<code>}&lt;/p&gt;</code>

<code>25</code>

<code>&lt;p&gt;</code><code>public</code> <code>static</code> <code>void</code> <code>main(string... args)</code><code>throws</code> <code>interruptedexception {&lt;br /&gt;</code>

<code>26</code>

<code>    </code><code>int</code> <code>looptimes = integer.parseint(args[</code><code>0</code><code>]);&lt;br /&gt;</code>

<code>27</code>

<code>    </code><code>mytask task =</code><code>new</code> <code>mytask(looptimes);&lt;br /&gt;</code>

<code>28</code>

<code>    </code><code>thread thread =</code><code>new</code> <code>thread(task);&lt;br /&gt;</code>

<code>29</code>

<code>    </code><code>thread.setdaemon(</code><code>true</code><code>);&lt;br /&gt;</code>

<code>30</code>

<code>    </code><code>thread.start();&lt;br /&gt;</code>

<code>31</code>

<code>    </code><code>timeunit.milliseconds.sleep(</code><code>100</code><code>);&lt;br /&gt;</code>

<code>32</code>

<code>    </code><code>task.running =</code><code>false</code><code>;&lt;br /&gt;</code>

<code>33</code>

<code>    </code><code>for</code> <code>(</code><code>int</code> <code>i =</code><code>0</code><code>; i &amp;lt;</code><code>200</code><code>; i++) {&lt;br /&gt;</code>

<code>34</code>

<code>        </code><code>timeunit.milliseconds.sleep(</code><code>500</code><code>);&lt;br /&gt;</code>

<code>35</code>

<code>        </code><code>system.out.println(</code><code>"stopped = "</code> <code>+ task.stopped);&lt;br /&gt;</code>

<code>36</code>

<code>        </code><code>if</code> <code>(task.stopped)&lt;br /&gt;</code>

<code>37</code>

<code>            </code><code>break</code><code>;&lt;br /&gt;</code>

<code>38</code>

<code>39</code>

<code>}&lt;br /&gt;</code>

this code repeatedly performs some work which has no impact on memory. the only difference it makes is how long it takes. by taking longer, it determines whether the code in run() will be optimised before or after running is set to false.

if i run this with 10 or 100 and -xx:+printcompilation i see

if i run this with 1000 you can see that the run() hasn’t been compiled and the thread stops

once the thread has been compiled, the change is never seen even though the thread will have context switched etc. many times.

the simple solution is to make the field volatile.  this will guarantee the field’s value consistent, not just eventually consistent which is what the cache might do for you.

while there are many examples of question like; why doesn’t my thread stop? the answer has more to do with java memory model which allows the jit “inline” the fields that it does the hardware and having multiple copies of the data in different caches.