Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31317

Re: VERY PECULIAR PROBLEM

From Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: VERY PECULIAR PROBLEM
Date 2015-03-12 22:52 -0500
Organization A noiseless patient Spider
Message-ID <mdtmt9$3te$1@dont-email.me> (permalink)
References <f9da1c67-a91e-4ec6-8199-137af1736453@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

VERY PECULIAR PROBLEM Doug Mika <dougmmika@gmail.com> - 2015-03-12 11:38 -0700
  Re: VERY PECULIAR PROBLEM Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-03-12 14:57 -0400
  Re: VERY PECULIAR PROBLEM Robbie Brown <rob@example.com> - 2015-03-12 19:29 +0000
  Re: VERY PECULIAR PROBLEM Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2015-03-12 22:52 -0500
  Re: VERY PECULIAR PROBLEM Nigel Wade <nmw@ion.le.ac.uk> - 2015-03-13 09:36 +0000
    Re: VERY PECULIAR PROBLEM Robbie Brown <rob@example.com> - 2015-03-13 10:12 +0000
      Re: VERY PECULIAR PROBLEM Robert Klemme <shortcutter@googlemail.com> - 2015-03-13 21:40 +0100

csiph-web