The Timer class is thread safe according to its javadoc ,
Therefore, it is enough to declare timer as volatile .
However, if a timer not assigned anywhere (as is the case), the best solution is to declare the field as final . This is enough to ensure that the timer variable can be safely used from multiple threads without further synchronization. (This is guaranteed by JLS Section 17.5 .)
If the Timer was not thread safe, you will need to perform all the actions in the Timer instance in the synchronized method or block (or the equivalent implemented using Locks, etc.). A timer declaration for volatile or final will NOT suffice.
source share