<story><title>Why does this Java program terminate despite appearing that it shouldn&apos;t?</title><url>http://stackoverflow.com/questions/16159203/why-does-this-java-program-terminate-despite-that-appears-that-it-shouldt-and</url></story><parent_chain></parent_chain><comment><author>rayiner</author><text>One of my biggest WTF moments as a programmer was dealing with a high performance spectrum analyzer. It was a cost no object bit of experimental military hardware, and was very impressive from that perspective. But the software was buggy. We would see lockups, as well as discontinuities in the readouts, which were screwing up our post-hoc data analysis. When I dug into the code, I realized that the program used several different threads, but there was not a lock or other synchronization mechanism in sight. You had a thread pulling data off a socket from the device and storing it in a (single) memory buffer, a thread writing commands through a socket to the device, a thread pulling data out of the buffer to draw it to a waterfall plot, and a thread writing data to disk. The lockups happened because the device didn&amp;#x27;t like to change the scan state while reading data, and there was no synchronization between the command thread and the readout thread. The discontinuities happened because there was a single buffer shared by the readout thread and the write out threads, with no synchronization. So as one thread was writing out a sample, the readout thread was replacing part of that data with the next sample.&lt;p&gt;I remember grepping the source for pthread_mutex or pthread_cond and slowly realizing that whoever wrote the code didn&amp;#x27;t even see the need for locks. To be fair, I later heard it was some poor hardware guy who knew some C who got roped into writing the software as an afterthought to show off the awesome hardware, a common problem with hardware companies.</text></comment>
<story><title>Why does this Java program terminate despite appearing that it shouldn&apos;t?</title><url>http://stackoverflow.com/questions/16159203/why-does-this-java-program-terminate-despite-that-appears-that-it-shouldt-and</url></story><parent_chain></parent_chain><comment><author>boothead</author><text>How can anyone program sanely in the presence of this:&lt;p&gt;currentPos = new Point(currentPos.x+1, currentPos.y+1); does a few things, including writing default values to x and y (0) and then writing their initial values in the constructor. Since your object is not safely published those 4 write operations can be freely reordered by the compiler &amp;#x2F; JVM.&lt;p&gt;So from the perspective of the reading thread, it is a legal execution to read x with its new value but y with its default value of 0 for example. By the time you reach the println statement (which by the way is synchronized and therefore does influence the read operations), the variables have their initial values and the program prints the expected values.&lt;p&gt;I&amp;#x27;m not anywhere near smart or careful enough for that... I think I&amp;#x27;ll stick with Haskell.</text></comment>