• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constant Checking

 
Mathew Mintalm
Ranch Hand
Posts: 103
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21551
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 12263
36
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Mathew Mintalm
Ranch Hand
Posts: 103
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder what the reasons behind this rather odd requirement might be.
 
Paul Clapham
Sheriff
Posts: 21551
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic