Hi I'm doing a java project in GUI in which I need to change something after a specific time passes I wanted to know the Differences between Timer and TimerTask classes and if they are both Thread or not ... also wanted to know if java.util.Timer and java.swing.Timer are different in such purpose? thanks for answering
-
1Also see: http://stackoverflow.com/questions/25025715/javax-swing-timer-vs-java-util-timer-inside-of-a-swing-application – px06 Apr 24 '17 at 14:20
-
Always use `javax.swing.Timer` for a Swing program. There are very few situations where you would use something else. – Radiodef Apr 24 '17 at 14:23
1 Answers
You can find all your answers at API docs. I.e. javax.swing.Timer
docs says:
In v 1.3, another Timer class was added to the Java platform: java.util.Timer. Both it and javax.swing.Timer provide the same basic functionality, but java.util.Timer is more general and has more features. The javax.swing.Timer has two features that can make it a little easier to use with GUIs. First, its event handling metaphor is familiar to GUI programmers and can make dealing with the event-dispatching thread a bit simpler. Second, its automatic thread sharing means that you don't have to take special steps to avoid spawning too many threads. Instead, your timer uses the same thread used to make cursors blink, tool tips appear, and so on.
The short answer is that the java.util.Timer
is more general.
Also, if you check the API pages, neither of those classes extend Thread
. Some of them useThreads` internally though.
If you really need to implement some sort of timing, I suggest to use a central ScheduledThreadPoolExecutor
. This scheduled executor is more flexible than any of the Timers, also, it gives you more control.

- 7,008
- 5
- 32
- 49