I 'm listening rs232Com using Portcom event-listener then generating propertychangelistener for interface controllers and everything works fine.
My new problem is that acquisitions for some sensors (anemometer) can terminate without any particular indication ( Pulse response ) and mostly dependent of bearings used.
The only solution i can see is that after an amount of time without anymore acquisition ( 2000ms ) i would like to end records to avoid recording data due to involuntary sensor handling .
At this point i can stop acquisitions unregistering controller of new message listener list using a button but i would like to do that automatically.
The point that miss me is to create a Timer that could launch a task after his delay timed out.This with a re-init function to feed him at each acquisition, barely the same way a watchdog works.
I started to search on web but i didn't find solutions and moreover which direction to go to
-Timer class
-Using a Thread / Runnable
-Schedule
-Modified watchdog
Thanks in advance
For this kind of problem, I often go with a custom thread that has a timer as attribute. The thread check its timer regularly and do something when it ends. And in my main I can add time to the timer attribute if I need to continue.
private static class CustomThread extends Thread {
private static int wait = 1000;
private int timer = 0;
public DeleteThread(int timer) {
this.timer = timer;
}
public void addToTimer(int time) {
this.timer += time;
}
public void run() {
while(this.timer > 0) {
try {
Thread.sleep(wait);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.timer -= wait;
}
//timer as ended
doSomething();
}
}
public class Main {
public static void main(String[] args) {
CustomThread myThread = new CustomThread(2000);
myThread.start();
//someCode
//Here I need my thread to continue for 1000 more
myThread.addToTimer(1000);
}
}