Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: =?UTF-8?Q?Joshua_Cranmer_=f0=9f=90=a7?= Newsgroups: comp.lang.java.programmer Subject: Re: VERY PECULIAR PROBLEM Date: Thu, 12 Mar 2015 22:52:51 -0500 Organization: A noiseless patient Spider Lines: 34 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 13 Mar 2015 03:52:09 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="8cedddb29338592f734c41b16a68fadc"; logging-data="4014"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+0UfvVP0ck8VRJuxTxBNHGq2o65tUiGXg=" User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:36.0) Gecko/20100101 Thunderbird/36.0 In-Reply-To: Cancel-Lock: sha1:T3W8HLFYR9wcTOpFeCAMEmWMcXw= Xref: csiph.com comp.lang.java.programmer:31317 On 3/12/2015 1:38 PM, Doug Mika wrote: > Hi to all, I don't know what is wrong with the following method. It > is a method inside a Timer class that extends Thread. The peculiar > problem is that IF i include the System.out.print(""); line in the > method, the program works FINE, if however I delete the > System.out.print(""); methods or REM it out, the program doesn't > count time? How Could that one line which does NOTHING cause the > entire program to function correctly? I don't know. Your test case contains insufficient details to let us do anything else other than wildly guess at what your code is doing. For all I know, it's supposed to be training a hyena to chew off your balls and it's instead training it to chew off your nose. More seriously though, despite the almost total lack of information, I'm going to guess that the problem is that paused (and probably secCount too, come to think of it) is not declared in the class to be a volatile variable. That, or some sort of lack of synchronization (of which volatile is a form of synchronization, but I digress), or possibly a timing issue. To answer your "how" question (which is still instructive even if my guess is completely wrong), the JIT compiler is free to do whatever it wants so long as it preserves observable behavior. In this case, the compiler would see an infinite loop and variables that are written to but never read. If the variables are not declared volatile, then there is no guarantee that they will ever be read or written to if accessed from multiple threads unless you provide some sort of synchronization. A call to System.out.print very possibly could cause that sort of synchronization, at least as far as the compiler knows. -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth