File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Constant Checking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constant Checking" Watch "Constant Checking" New topic

Constant Checking

Mathew Mintalm
Ranch Hand

Joined: Feb 21, 2010
Posts: 103

Hello, I have quite simple question I guess, but unfortunately I couldn't find proper solution.

Oh, here's my question.

And from time to time this int will be changed somehow (doesn't matter how, its just sample)

Now my question is, how to create some kind of "constant checking" method.

Where for example code will check each second if this int == XYZ.

Thanks for help!
Paul Clapham

Joined: Oct 14, 2005
Posts: 19845

You could use a java.util.Timer which did that once a second. But you called this a "constant"... If that's the case then why not just declare it as a constant? Then this strange requirement goes away.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11913

I'm not sure how you could do it with just a primitive, but you could create a wrapper, and implement the observer pattern. Then, you don't have to check every second, but instead have it alert whatever needs to know when it changes.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
marc weber

Joined: Aug 31, 2004
Posts: 11343

Paul's suggestion of the Timer seems like what you're asking for.

But instead of "constantly checking" to see if the variable has changed, you might consider making the variable private, with the only way to change it through a setter method. Then whenever the variable is changed, that setter method could also send notifications.

(Paul: I think "constant" is used here to mean "continuously" checking the value.)

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Mathew Mintalm
Ranch Hand

Joined: Feb 21, 2010
Posts: 103

Hmm, thanks for answers, anyway I have tried to make my own solution, please tell me what do you guys think.

New thread:

And after 2s I can receive "Works after 2s!", and it works as it should, i can use this way to check smething each X seconds.

Do you guys think it will be good way?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63529

I wonder what the reasons behind this rather odd requirement might be.

[Asking smart questions] [About Bear] [Books by Bear]
Paul Clapham

Joined: Oct 14, 2005
Posts: 19845

Mathew Mintalm wrote:Do you guys think it will be good way?

Not really. For one thing you have no way of stopping that thread. But on the other hand it will stop on its own, by crashing, as soon as the call stack overflows.

However all of this discussion is more or less meaningless until the base question of why on earth you would want to check a variable every 2 seconds is answered.
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
The proper way to do this is to let whatever function that changes the value of the variable, notify of the change in some way. If you need a separate thread just to check up on a single variable, you are not writing your code in a good way.
subject: Constant Checking
jQuery in Action, 3rd edition