iphone - 使用 threadDictionary 这段代码线程安全吗?

标签 iphone ios objective-c

+(NSString*)FromNumber:(NSNumber*)number
{
    NSString *key = akey;
    NSNumberFormatter *numberFormatter = [[[NSThread currentThread] threadDictionary] objectForKey:key];
    if(!numberFormatter)
    {
        numberFormatter = [self getDecimalNumberFormatterWithDigits:2];
        [numberFormatter setFormatterBehavior:NSNumberFormatterPercentStyle];

        [numberFormatter setNegativeSuffix:@"%"];
        [numberFormatter setPositiveSuffix:@"%"];
        [numberFormatter setNilSymbol:@""];
        [numberFormatter setLocale:[ISMDateUtilities enUSPosixLocale]];

        [[[NSThread currentThread] threadDictionary] setValue:numberFormatter forKey:key];
    }
    return [numberFormatter stringForObjectValue:number];
}

我很好奇的是使用threadDictionary来同步代码块?

谢谢!

最佳答案

此代码的线程安全性取决于 akey 是否为常量。如果是,那么是的,这个方法是线程安全的,因为它不在线程之间共享任何状态。 (不过,我不确定你所说的“同步”是什么意思——据我所知,这里没有同步。)

关于iphone - 使用 threadDictionary 这段代码线程安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17980074/

相关文章:

iphone - 获取 NSDictionary 的字符串

iphone - 为什么我的应用程序内浏览器无法加载?

ios - 通过企业账户分发Apple Pay应用

objective-c - 绘制虚线和实线

ios - 错误 : couldn't get default input device. .. 在模拟器上运行时

iPhone XML请求

ios - 当我尝试删除 UITableView 部分时 EXC_BAD_ACCESS

iphone - UML:如何为移动应用程序(iPhone/iOS、Android)建模 UI 和后台线程

ios - 属性类型与访问器类型不匹配

ios - 将 NSNumber 转换为 Boolean 会为 iPhone 和 iPad 提供不同的结果