Java Timer vs Executor Service

0 votes

Can anyone explain the benefits of using java.util.timer and ExecutorService?

Also, do mention if you have faced any kind of issues while using them.

Jul 26, 2018 in Java by v.liyyah
• 1,300 points
1,129 views

1 answer to this question.

0 votes

According to Java Concurrency in Practice:

  • Timer is sensitive to changes in the system clock whereas ScheduledThreadPoolExecutor isn't.

  • Timer has just one execution thread, thus the long-running task tends to delay other tasks. While ScheduledThreadPoolExecutor can be configured with n number of threads. Moreover, you can have full control over created threads by providing ThreadFactory.

  • Runtime exceptions thrown in TimerTask will kill that one thread, thus making Timer dead. ScheduledThreadExecutor catches runtime exceptions and lets you handle them if you want by overriding afterExecute method from ThreadPoolExecutor. Only the task which threw exception will be canceled, but other tasks will continue to run.

I would suggest you use ScheduledThreadExecutor instead of Timer.

Also, note that ScheduledThreadExecutor is not available in Java 1.4 library, but there is a Backport of JSR 166 (java.util.concurrent) to Java 1.2, 1.3, 1.4, which has the ScheduledThreadExecutor class.

answered Jul 26, 2018 by code.reaper12
• 3,450 points

Related Questions In Java

0 votes
1 answer

concat() vs “+” operator : In Java for String concatenation

Basically, there are two important differences between ...READ MORE

answered Apr 27, 2018 in Java by Rishabh
• 3,540 points
1,276 views
0 votes
1 answer

How to set a timer in Java?

To work on timer in java, you ...READ MORE

answered May 16, 2018 in Java by Daisy
• 8,050 points
68 views
0 votes
2 answers

C++ performance vs. Java/C#

The overhead of interpreting code is much higher than ...READ MORE

answered Dec 27, 2018 in Java by iangregor
• 300 points
77 views
0 votes
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,640 points
564 views
0 votes
1 answer

Java execute vs executeQuery vs executeUpdate

I think the major differences between execute, executeQuery, ...READ MORE

answered Jul 30, 2018 in Java by geek.erkami
• 2,640 points
6,971 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 11,366 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,430 points
2,430 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,430 points
6,270 views
0 votes
1 answer

Comparable vs Comparator in Java

I think you are not alone who is ...READ MORE

answered May 10, 2018 in Java by code.reaper12
• 3,450 points
159 views
0 votes
1 answer

revalidate() vs repaint() in Java Swing

You need to call repaint() and revalidate() both in order ...READ MORE

answered Sep 19, 2018 in Java by code.reaper12
• 3,450 points
1,057 views