<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 11 Mar 2019, at 10:59, Mark Evenson <<a href="mailto:evenson@panix.com" class="">evenson@panix.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><br class=""><br class=""><blockquote type="cite" class="">On Mar 11, 2019, at 06:38, dingd <<a href="mailto:ntysdd@qq.com" class="">ntysdd@qq.com</a>> wrote:<br class=""><br class="">this is recommended in the manual, it contains a race condition.<br class=""><br class="">Interpreter interpreter = Interpreter.getInstance ();<br class="">if ( interpreter == null ) {<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span>interpreter = Interpreter.createInstance ();<br class="">}<br class=""><br class="">getInstance and createInstance should be swapped.<br class=""></blockquote><br class="">Since both Interpreter.getInstance() and Interpreter.createInstance() are<br class="">methods synchronized on the same object monitor, I don’t see that there is a<br class="">race condition here. Could you explain a little more about your reasoning<br class="">and/or experience with the race condition?<br class=""></div></div></blockquote></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Two threads could call getInstance and have interpreter=null.</div><div class="">Then both will call createInstance, thus two instances will be created.</div><div class="">If interpreter is a local binding as it appears in the code above, then it is what’s intended and all is good.</div><div class="">But if interpreter is a global binding and you expected interpreter to have a single instance, then it’s wrong.</div><div class=""><br class=""></div><br class=""><div class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">-- <br class="">__Pascal J. Bourguignon__</div><div style="color: rgb(0, 0, 0); letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><br class=""><br class=""></div></div></div>
</div>
<br class=""></body></html>