在哪里创建和使用 ScheduledThreadPoolExecutor、TimerTask 或 Handler?

发布于 2024-12-15 04:57:19 字数 483 浏览 0 评论 0原文

我需要让我的 RSS Feed 阅读器每 10 分钟检查一次 feed 中是否有新帖子,然后在有新帖子时解析它们。我还需要大约每分钟更新一次用户界面。

我从不同的来源读到和听到了不同的事情。我目前的理解是,我可以使用 ScheduledThreadPoolExecutor 来创建两个调度线程,其中一个需要一个 Handler 来更新 UI。我不确定这些类或 TimerTask 的最有效使用方式是什么。

我也非常不确定在哪里创建这些的子类。一位朋友建议将 TimerTask 扩展为我的 FeedParser 类中的内部类,以使其更简单。但是,要以这种方式实现它,我必须使用 TimerTaskrun() 方法而不覆盖它,这意味着我不能简单地使用我需要的参数需要运行的功能。

简而言之,为此安排任务的最佳方法是什么?我将在哪里实施这些任务?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝天 2024-12-22 04:57:19

我更喜欢使用 ScheduledThreadPoolExecutor。一般来说,如果我正确理解您的要求,所有这些都可以在您的活动中实现,不需要 TimerTask 和 Handler,请参阅下面的示例代码:

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()
}

记住在 Activity.onDestroy() 中正确完成/关闭您的可运行任务,希望有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文