Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I create and use ScheduledThreadPoolExecutor, TimerTask, or Handler?

I need to make my RSS Feed reader check the feed every 10 minutes for new posts, and then parse them if there are new ones. I also need to update the UI about every minute.

I have read and heard different things from various sources. My current understanding is that I can use ScheduledThreadPoolExecutor to make two scheduled threads, and one of them needs a Handler for updating the UI. I am unsure about what the most efficient use of these classes or TimerTask.

I am also very uncertain about where to make subclasses of these. One friend suggested extending TimerTask as an inner class in my FeedParser class to make it simpler. However, to implement it in that way, I have to use the run() method for TimerTask without overriding it, meaning I can't simply use the parameters I need for the functions that need to run.

In short, what is the best way to schedule the tasks for this, and where would I implement these?

like image 627
zr00 Avatar asked Nov 11 '11 19:11

zr00


People also ask

What is the difference between Android timer and a handler to do action every n seconds?

Handler is better than TimerTask . The Java TimerTask and the Android Handler both allow you to schedule delayed and repeated tasks on background threads. However, the literature overwhelmingly recommends using Handler over TimerTask in Android (see here, here, here, here, here, and here).

What is ScheduledThreadPoolExecutor in Java?

ScheduledThreadPoolExecutor class in Java is a subclass of ThreadPoolExecutor class defined in java. util. concurrent package. As it is clear from its name that this class is useful when we want to schedule tasks to run repeatedly or to run after a given delay for some future time. It creates a fixed-sized Thread Pool.

How can you perform repeated tasks in a service in Android?

There are at least four ways to run periodic tasks: Handler - Execute a Runnable task on the UIThread after an optional delay. ScheduledThreadPoolExecutor - Execute periodic tasks with a background thread pool. AlarmManager - Execute any periodic task in the background as a service.


1 Answers

I prefer to use ScheduledThreadPoolExecutor. Generally, if I understand your requirements correctly, all these can be implemented in your activity, TimerTask and Handler are not needed, see sample code below:

public class MyActivity extends Activity {   private ScheduledExecutorService scheduleTaskExecutor;    public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     scheduleTaskExecutor= Executors.newScheduledThreadPool(5);      // This schedule a task to run every 10 minutes:     scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {       public void run() {         // Parsing RSS feed:         myFeedParser.doSomething();          // If you need update UI, simply do this:         runOnUiThread(new Runnable() {           public void run() {             // update your UI component here.             myTextView.setText("refreshed");           }         });       }     }, 0, 10, TimeUnit.MINUTES);   } // end of onCreate() } 

Remember to finish/close your runnable task properly in Activity.onDestroy(), hope that help.

like image 169
yorkw Avatar answered Sep 19 '22 18:09

yorkw