- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
根据 MSDN,Stopwatch
类实例方法对于多线程访问是不安全的。这也可以通过检查各个方法来确认。
但是,由于我在代码中的几个地方只需要简单的“已用时间”计时器,我想知道它是否仍然可以无锁地完成,使用类似的东西:
public class ElapsedTimer : IElapsedTimer
{
/// Shared (static) stopwatch instance.
static readonly Stopwatch _stopwatch = Stopwatch.StartNew();
/// Stopwatch offset captured at last call to Reset
long _lastResetTime;
/// Each instance is immediately reset when created
public ElapsedTimer()
{
Reset();
}
/// Resets this instance.
public void Reset()
{
Interlocked.Exchange(ref _lastResetTime, _stopwatch.ElapsedMilliseconds);
}
/// Seconds elapsed since last reset.
public double SecondsElapsed
{
get
{
var resetTime = Interlocked.Read(ref _lastResetTime);
return (_stopwatch.ElapsedMilliseconds - resetTime) / 1000.0;
}
}
}
因为 _stopwatch.ElapsedMilliseconds
基本上是对 QueryPerformanceCounter
的调用,我假设从多线程调用是安全的?与常规 Stopwatch
的不同之处在于,此类基本上一直在运行,所以我不需要保持任何附加状态(“运行”或“停止”),如 秒表
可以。
(更新)
在@Scott 在下面的回答中提出建议后,我意识到 Stopwatch
提供了一个简单的静态 GetTimestamp
方法,它返回原始的 QueryPerformanceCounter
蜱虫。也就是说,代码可以修改成这样,是线程安全的:
public class ElapsedTimer : IElapsedTimer
{
static double Frequency = (double)Stopwatch.Frequency;
/// Stopwatch offset for last reset
long _lastResetTime;
public ElapsedTimer()
{
Reset();
}
/// Resets this instance.
public void Reset()
{
// must keep in mind that GetTimestamp ticks are NOT DateTime ticks
// (i.e. they must be divided by Stopwatch.Frequency to get seconds,
// and Stopwatch.Frequency is hw dependent)
Interlocked.Exchange(ref _lastResetTime, Stopwatch.GetTimestamp());
}
/// Seconds elapsed since last reset
public double SecondsElapsed
{
get
{
var resetTime = Interlocked.Read(ref _lastResetTime);
return (Stopwatch.GetTimestamp() - resetTime) / Frequency;
}
}
}
澄清一下,这段代码的想法是:
我会像这样使用它:
private readonly ElapsedTimer _lastCommandReceiveTime = new ElapsedTimer();
// can be invoked by multiple threads (usually threadpool)
void Port_CommandReceived(Cmd command)
{
_lastCommandReceiveTime.Reset();
}
// also can be run from multiple threads
void DoStuff()
{
if (_lastCommandReceiveTime.SecondsElapsed > 10)
{
// must do something
}
}
最佳答案
我建议的唯一更改是使用 Interlocked.Exchange(ref _lastResetTime, _stopwatch.ElapsedTicks);
而不是 Milliseconds 因为如果您处于高性能模式,则可以从查询性能计数器
。
关于c# - 简单的无锁秒表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37799650/
我正在学习如何开发,我正在尝试使用 Javascript 为我的 html 正文实现秒表 var c = 0.0; function tempo() { document.getElementBy
我正在尝试使用 JavaScript 制作一个秒表。我有日期来填充正确的信息,但我的秒表不工作。我单击开始,数字永远不会从 0 移动,我想让它在 MS 中立即递增。我也有 JS 和 HTML 的代码。
我正在制作一个简单的秒表。我面临的问题是当我再次开始时从上次停止的地方开始/恢复秒表。我试图保留耗时的值,但没有运气 演示= import UIKit class ViewController: UI
我一直在尝试使用 JavaScript 创建一个简单的秒表脚本,以显示已经过去的秒数、分钟数和小时数。 理想情况下,我希望时间显示如下: hh:mm:ss 使用 JavaScript,我无法找到一种内
大家好,我最近开始学习 React,但遇到了一些问题。我正在尝试制作简单的 react 应用程序,我正在制作的组件之一是秒表。现在,我在使用从父组件传递给秒表组件的 Prop 时遇到问题。这是我的应用
我有一个代码可以正常工作,但我认为如果我继续遵循相同的策略来做所有相同的事情,它会无缘无故地在处理器上过载 我有一个代表时间的变量,这个变量在游戏对象的速度高于 0.5f 的每一帧中都为 0,所以我不
我想创建一个跟踪任务时间的 ASP.NET WinForms 应用程序。我需要能够编写表单,以便将任务添加到数据库、在新选项卡中打开它,以及能够开始、暂停和停止任务。完成后,我需要计算完成任务所需的时
我正在使用我在此处找到的 stopwatch 代码: http://www.kellishaver.com/projects/stopwatch/ (function($) { $.fn.st
本文实例讲述了java实现的计时器【秒表】功能。分享给大家供大家参考,具体如下: 应用名称:java计时器 用到的知识:java gui编程 开发环境:win8+eclipse+jdk1.8
众所周知秒表 在将在所有内核上运行的多线程异步应用程序中可能存在问题。 On a multiprocessor computer, it does not matter which processor
我想知道测量 C++ 中某些代码的执行时间的最佳方法是什么。是否有内置的秒表(.Net)类似类?我正在VS2010上开发C++。我如何(如果)在我的 C++ 项目中使用 .Net 库?先感谢您。 最佳
我已经实现了一个 SIP Servlet,其中我从客户端接收两种类型的消息。我可以接收高优先级消息和低优先级消息,当我读取消息的 URI 时,我将其分开,如下面的代码所示。我必须实现一个基本的秒表,它
我使用pyqt库在python中制作了一个秒表(chronometre),但是当我按下重置按钮然后按下开始按钮时遇到问题,我看到时间增加了两倍,然后重复我看到增加的过程三人一组等等。 我不明白为什么会
我在 jQuery 中发现了这个有用的 JS,用于具有小时、分钟和秒的秒表。 http://daokun.webs.com/jquery.stopwatch.js 问题是,小时计数器对我来说毫无用处,
我想从我的应用程序启动 android 附带的秒表应用程序,但不知道如何执行此操作。我不想自己创建秒表,因为它需要在我关闭我的应用程序时继续运行。 最佳答案 在 Android 4.1(或可能是 4.
我正在开发一个简单的秒表,问题是当用户按下主页按钮时,线程女巫计算时间卡住我如何避免让线程在后台运行? 最佳答案 一个更简单的解决方案是使用 system.currentTimeMillis 记录秒表
我这里有秒表所需的代码。我想要的只是摆脱这里的 Swing 部分并在控制台中显示相同的输出。有人可以帮忙吗? import java.awt.event.*; import javax.swing.*
我正在编写一个用于计时的秒表应用程序,我需要检查时钟是否超过某个时间间隔(例如 20:00 - 06:00),在这种情况下,将当前耗时添加到我设置的 TimeInterval 变量中稍后将存储在数据库
我正在使用 NSTimer 制作秒表。如果用户切换到不同的应用程序,我希望它继续工作,但现在它只在应用程序运行时工作。我认为通过在计时器启动时记录时间戳来解决这个问题可能非常简单,但我不确定如何将其与
我正在尝试制作一个简单的 Java 应用程序来计算时间,并能够停止和启动计时器。但是,标签不会更新,当我按下开始时,它会卡住。 你能帮我找出问题所在吗? package random; import
我是一名优秀的程序员,十分优秀!