Timer and TimerTask usage | Telit Cinterion IoT Developer Community
February 9, 2017 - 11:13am, 2698 views
I have time scheduled events in my application and I'm using Timer and TimerTask classes for this purpose.
I didn't see any example or question about this issue on forum.
Is there any other way of realizing time scheduled events without using this pair of classes? (Except Thread.sleep() method.)
Is there an application note about how to use these classes?
I don't know how jvm creates and uses timers at low level but embedded devices has limited number of timers in their architecture. Thus, a limit is necessary for Timer object creation excluding memory constraints.
So, is there a limit for Timer object creation for modules, in my case BGS5? I'm using 6 Timer object always alive in my application and each Timer object is schedules a separate TimerTask object. That is, 6 Timer and 6 TimerTask object. Is this a proper usage? Maybe only one Timer is enough for all TimerTasks.
Timer and TimerTask classes are a part of standard Java API. The documentation doesn't specify how many instances of a certain class can be created on a device. For each Timer instance a separate thread is created in the background. As Java is not the low level language and the Timer class has no 1:1 relation to any low level timers. According to the API "This class does not offer real-time guarantees: it schedules tasks using the Object.wait(long) method". So this is only a high level API that is based on other Java methods.
I think that your approach is good.
Thanks for the answer.
I thought maybe a Timer object and a low level timer is connected to each other. There is a relation like this between an ATCommand instance and AT Parser. In ATCommand javadoc it is written:
Because the instances of this ATCommand class are connected directly to the different AT parsers inside the module, there are only exactly as much instances possible as the number of provided AT interpreters. Trying to create more instances of this class will cause the constructor to fail.
It is true for ATCommand class and AT parsers but not for Timer and TimerTask.