首页 \ 问答 \ 单身和线程安全?(Singleton and Thread Safety?)

单身和线程安全?(Singleton and Thread Safety?)

我目前正在开发一个“Singleton”类,但它必须承受线程安全,因为我不仅访问,而且设置一些东西..我想知道我怎么能这样 - 因为它似乎锁定的东西与当我不知道它在做什么以及我是否必须使每个方法线程都安全时, lock(object)感觉有点hacky?

到目前为止,我有这个非线程安全的代码 - 因为在每个线程中应该访问相同的数据。 我怎样才能最好地使它保持线程安全并保持单身?

public class AppSession() {
    private static AppSession _instance = new AppSession();
    public static AppSession Instance { get { return _instance; } }

    private AppSession() { }

    private string _actionName = "none";
    private DateTime? _actionTime = null;        
    public void ActionExecuted(string action) {
        _actionName = action ?? String.Empty;
        _actionTime = DateTime.UtcNow;
    }

    public LastAction {
       get { return $"{_action} at {_actionTime?.Value.ToString()}"; }
    }
}

I'm currently developping a "Singleton" class, but it has to withstand thread safety as I'm not only accessing, but also settings some stuff.. I was wondering how I could to this - as it seems the locking of stuff with lock(object) feels somewhat hacky when I don't know what it's actually doing and if I have to make every single method thread safe as well?

So far I have this non thread safe code - as in each thread should access the same data. How would I best go about making it thread safe and keep it a singleton?

public class AppSession() {
    private static AppSession _instance = new AppSession();
    public static AppSession Instance { get { return _instance; } }

    private AppSession() { }

    private string _actionName = "none";
    private DateTime? _actionTime = null;        
    public void ActionExecuted(string action) {
        _actionName = action ?? String.Empty;
        _actionTime = DateTime.UtcNow;
    }

    public LastAction {
       get { return $"{_action} at {_actionTime?.Value.ToString()}"; }
    }
}

原文:https://stackoverflow.com/questions/42470041
更新时间:2023-08-14 07:08

最满意答案

不幸的是,似乎kashidaCSS3文本中删除并延迟了CSS4文本 ,所以如果你想用Kashida证明你的文本,你应该尝试移植harfbuzz-old 添加kashida算法 (它的算法从新一代的harfbuzz, ref中删除),这是基于此处存档的Microsoft规范。 这是Microsoft的kashida插入优先级表,您应该尝试以某种方式实现: 在此处输入图像描述

添加Kashida(«»)用于HTML本身的文本样式并不是一个好主意(因为如果用户复制文本它将包含添加的Kashidas,它将使浏览器在页面中找到问题)但是当前webkit bug / blink bug (Safari / Chrome)版本不支持在内联( )元素期间加入草书脚本字符(如阿拉伯语/波斯语),似乎没有任何机会在不插入Kashida的情况下获得此类理由(或者您应该通过以下方式处理oncopy事件你自己有自己的问题)。

有趣的是,微软正在扩展IE Kashida理由实施,以支持更有趣的事情,如text-kashida-space (配置kashida与白色空间扩展的比率,同时文本证明)。 似乎我们必须再等十年才能在所有主流浏览器中实现这些功能。


Unfortunately seems kashida is dropped from CSS3 text and deferred for CSS4 text, so if you want justify your text with Kashida, you should try porting harfbuzz-old adding kashida algorithm (that its algorithm is removed from newer generation of harfbuzz, ref) which is based on a Microsoft specification that is archived here. Here is Microsoft's kashida insertion priority table that you should try to implement somehow: enter image description here

ّAdding Kashida («ـ») for text styling on HTML itself is not a good idea (because if user copy the text it will contains added Kashidas and it will make problem for browsers find-in-page) however as current webkitbug/blinkbug (Safari/Chrome) versions do not support joining cursive script characters (like Arabic/Persian) during inline (pseudo)elements there seems there is not any chance you get this type of justification without inserting Kashida (or you should handle oncopy event by your own that has its own problems).

It is also interesting that Microsoft is extended IE Kashida justification implementation to support more interesting things like text-kashida-space (configure ratio of kashida to white space expansion while text justifying). Seems we must wait another decade to these get implemented across all major browsers.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。