精确一秒间隔的最佳计时器技术是什么?

发布于 2024-12-07 14:02:54 字数 1069 浏览 0 评论 0原文

我正在为一项专门的户外运动编写一个简单的计时应用程序。 该应用程序每秒执行一系列计算,并显示用户感兴趣的信息。经过的时间是计算结果准确性的关键。理想情况下,计算和结果每秒都会更新一次。我尝试过使用计时器或计时器小部件来控制更新间隔:

是否有更准确的方法来建立间隔? 有没有一种技术可以给我一个几乎精确的一秒间隔?

下面的代码并不完整 - 只是为了展示我正在使用的时间和天文钟的类型 - 它们运行良好,只是不精确。

import java.util.Timer ;
import java.util.TimerTask;
...
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
public void run() {
...
TimerMethod();
}
}, 0, 1000);
... 
private void TimerMethod()
{
this.runOnUiThread(Timer_Tick);
}
...
private Runnable Timer_Tick = new Runnable() {
public void run() {
blah blah
}

//////////////////////////////////////////////////////////////

import android.widget.Chronometer;
...
final Chronometer myChronometer = (Chronometer)findViewById(R.id.chronometer);
final TextView keyBox = (TextView)findViewById(R.id.keyBox);
...      
myChronometer.start();
...   
myChronometer.setOnChronometerTickListener(
new Chronometer.OnChronometerTickListener(){
...
public void onChronometerTick(Chronometer myChronometer) {
blah blah             
}

I am writing a simple timekeeping app for a specialized outdoor sport.
The app performs a series of calculations every second, and displays information of interest to the user. Elapsed time is key to the accuracy of the calculated results. Ideally the calculation and results would be updated exactly every second. I have tried using a timer or a chronometer widget to control the update interval:

Is there a more accurate way to establish the interval?
Is there a technique that will give me a nearly exact one second interval?

The code below is not complete - just an effort to show the type of time and chronometer I am using - they run fine, they are just not precise.

import java.util.Timer ;
import java.util.TimerTask;
...
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
public void run() {
...
TimerMethod();
}
}, 0, 1000);
... 
private void TimerMethod()
{
this.runOnUiThread(Timer_Tick);
}
...
private Runnable Timer_Tick = new Runnable() {
public void run() {
blah blah
}

///////////////////////////////

import android.widget.Chronometer;
...
final Chronometer myChronometer = (Chronometer)findViewById(R.id.chronometer);
final TextView keyBox = (TextView)findViewById(R.id.keyBox);
...      
myChronometer.start();
...   
myChronometer.setOnChronometerTickListener(
new Chronometer.OnChronometerTickListener(){
...
public void onChronometerTick(Chronometer myChronometer) {
blah blah             
}

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

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

发布评论

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

评论(1

不语却知心 2024-12-14 14:02:55

您是否查看过 java.util.Timer类的scheduleAtFixedRate方法?

根据javadoc,
“在固定速率执行中,每次执行都是相对于初始执行的计划执行时间来安排的。如果执行因任何原因(例如垃圾收集或其他后台活动)而延迟,则将快速连续发生两个或多个执行从长远来看,执行频率将恰好是指定周期的倒数(假设 Object.wait(long) 底层的系统时钟是准确的)。

Did you look at the scheduleAtFixedRate method of java.util.Timer class?

According to javadoc,
"In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up." In the long run, the frequency of execution will be exactly the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate)."

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