+(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/

10-11 09:19