如何使用 TimerTask 来运行线程? [英] How do you use a TimerTask to run a thread?

查看:22
本文介绍了如何使用 TimerTask 来运行线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力寻找 Android 上 TimerTask 函数的文档.我需要使用 TimerTask 每隔一段时间运行一个线程,但不知道如何去做.任何建议或示例将不胜感激.

I'm struggling to find documentation for the TimerTask function on Android. I need to run a thread at intervals using a TimerTask but have no idea how to go about this. Any advice or examples would be greatly appreciated.

推荐答案

您使用 Timer,当您使用任何 schedule 方法安排 TimerTask 时,它会自动为您创建一个新线程.

You use a Timer, and that automatically creates a new Thread for you when you schedule a TimerTask using any of the schedule-methods.

示例:

Timer t = new Timer();
t.schedule(myTimerTask, 1000L);

这会创建一个 Timer,它每秒在属于该 Timer 的线程中运行 myTimerTask.

This creates a Timer running myTimerTask in a Thread belonging to that Timer once every second.

这篇关于如何使用 TimerTask 来运行线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆