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
5,636 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,500 points

Related Questions In Java

+1 vote
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,620 points
4,441 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,120 points
984 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
938 views
+1 vote
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,680 points
3,336 views
0 votes
1 answer

Java execute vs executeQuery vs executeUpdate

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

answered Jul 31, 2018 in Java by geek.erkami
• 2,680 points
23,879 views
+5 votes
4 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,450 points

edited Nov 7, 2018 by Omkar 79,298 views
+1 vote
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,500 points
7,919 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,500 points
16,940 views
0 votes
1 answer

Comparable vs Comparator in Java

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

answered May 11, 2018 in Java by code.reaper12
• 3,500 points
858 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 20, 2018 in Java by code.reaper12
• 3,500 points
10,116 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP