做一次手术每100ms为1000毫秒 [英] Do an operation every 100ms for 1000 ms
问题描述
我想办一个操作每100ms为1000毫秒。
我相信我会需要使用
处理程序
我该怎么办呢?
TIMER T =新的Timer();
诠释计数= 0;
t.scheduleAtFixedRate(新的TimerTask(){
算上++;
//做的东西
如果(计数> = 10)
t.cancel();
},0,100);
这时间表定时器执行的TimerTask
,以0毫秒的延迟。它将执行的TimerTask
每100毫秒的身体。使用计数
来跟踪你在哪里工作,经过10次迭代,你可以取消计时器。
作为@ Jug6ernaut提到的,确保您的任务将不会需要很长时间来执行。冗长的任务(那些需要更长的时间超过100毫秒,你的情况),将导致滞后/潜在的不良后果。
I would like to do an operation every 100ms for 1000ms.
I believe I would need to use the
handler
How do I do that?
Timer t = new Timer();
int count = 0;
t.scheduleAtFixedRate(new TimerTask() {
count++;
// Do stuff
if (count >= 10)
t.cancel();
}, 0, 100);
This schedules a timer to execute a TimerTask
, with a 0 millisecond delay. It will execute the body of the TimerTask
every 100 milliseconds. Using count
to keep track of where you are in the task, after 10 iterations, you may cancel the timer.
As @Jug6ernaut mentioned, ensure your task won't take long to execute. Lengthy tasks (ones that take longer than 100 milliseconds, in your case) will cause lag/potentially undesirable results.
这篇关于做一次手术每100ms为1000毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!