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,290 points
424 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,520 points
593 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,040 points
35 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
45 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,320 points
236 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,320 points
2,942 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,360 points

edited Nov 6, 2018 by Omkar 5,345 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,050 points
1,414 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,050 points
3,876 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
86 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
374 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.