TimerTask的或处理程序

编程入门 行业动态 更新时间:2024-10-10 17:30:21
本文介绍了TimerTask的或处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好我有一个快速的问题。比方说,我想执行每10秒的一些行动,并不一定需要更新视图。现在的问题是:是否有更好的(我的意思是更加有效的)使用定时器的TimerTask喜欢这里:

Hi I have a quick question. Let's say that I want to perform some action every 10 seconds and it doesn't necessarily need to update the view. The question is: is it better (I mean more efficient and effective) to use timer with timertask like here:

final Handler handler = new Handler(); TimerTask timertask = new TimerTask() { @Override public void run() { handler.post(new Runnable() { public void run() { <some task> } }); } }; timer = new Timer(); timer.schedule(timertask, 0, 15000); }

或只是一个postdelayed处理程序

or just a handler with postdelayed

final Handler handler = new Handler(); final Runnable r = new Runnable() { public void run() { <some task> } }; handler.postDelayed(r, 15000);

此外,我将不胜感激,如果你能解释一下什么时候使用哪种方法,为什么其中一个比另一个更有效(如果它确实是)。谢谢你。

Also I would be grateful if you could explain when to use which approach and why one of them is more efficient than another (if it actually is). Thank you.

推荐答案

有使用定时器的一些缺点

There are some disadvantages of using Timer

它创建只有单个线程来执行任务,如果一个任务需要很长时间才能运行,其他任务受到影响。 它不处理抛出异常的任务和线程刚刚结束,这会影响其他计划的任务,他们从来没有运行

It creates only single thread to execute the tasks and if a task takes too long to run, other tasks suffer. It does not handle exceptions thrown by tasks and thread just terminates, which affects other scheduled tasks and they are never run

从复制:

TimerTask VS视频下载器,处理器postDelayed - ?最准确的调用函数每隔N毫秒

更多推荐

TimerTask的或处理程序

本文发布于:2023-11-27 13:17:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638232.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序   TimerTask

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!