我意识到在Java中,双重锁定由于内存模型存在缺陷,但通常与单例模式和优化单例创建相关联。
那么在Objective-C下呢?我有一个布尔标志来确定我的应用程序是否正在流数据。我有3个方法:startStreaming、stopStreaming、streamingDataReceived,并且我使用以下方式来保护它们免受多个线程的干扰:
那么在Objective-C下呢?我有一个布尔标志来确定我的应用程序是否正在流数据。我有3个方法:startStreaming、stopStreaming、streamingDataReceived,并且我使用以下方式来保护它们免受多个线程的干扰:
- (void) streamingDataReceived:(StreamingData *)streamingData {
if (self.isStreaming) {
@synchronized(self) {
if (self.isStreaming) {
- (void) stopStreaming {
if (self.isStreaming) {
@synchronized(self) {
if (self.isStreaming) {
- (void) startStreaming:(NSArray *)watchlistInstrumentData {
if (!self.isStreaming) {
@synchronized(self) {
if (!self.isStreaming) {
这个双重检查是否必要?在Objective-C中,双重检查是否存在与Java类似的问题?有哪些替代模式(反模式)可供选择。
谢谢