Hi im a beginner so sorry for my question if it sounds naive.
I want to implement a thread that runs in the background and listens all the time. By listening i mean, say it keeps check on a value returned from main thread and if the vaue exceeds certain figure, it executes some method, or say exits the program.
If you could give me some idea or at least refer me to something useful, that’ll be great.
You wouldn’t want this thread to run in a loop, continuously polling the value, because that would waste processing.
Ideally, the listener would be actively notified when the value had changed. This would require that any code that modifies the monitored value call a special method. It might not be necessary for the listener to run in a separate thread; it would depend on what the listener does when it is notified.
If the code that modifies the value cannot be changed, then the best you can do is to check the value at intervals. You won’t see a change immediately, and its possible that you could miss changes altogether because the value is changing multiple times during an interval.
Which of these solutions fits your situation the best?