多線程

1. GCD

  • 同步/異步 串行/併發
  • dispatch_barrier_async
  • dispatch_group
主隊列引發的循環等待(主隊列當中提交的任務,不管是同步/異步,都會在主線程中執行)
-(void)viewDidLoad {
    dispatch_sync(dispatch_get_main_queue(), ^{
        [self doSomething];
    });
}
複製代碼

-(void)viewDidLoad {
    dispatch_sync(serialQueue, ^{
        [self doSomething];
    });
}
複製代碼
子線程默認不開啓RunLoop,performSelector:withObject:afterDelay:不執行
-(void)viewDidLoad {
    dispatch_async(global_queue, ^{
        NSLoag(@"1");
        [self performSelector:@selector(printLog)
              withObjec:nil
              afterDelay:0];
        NSLog(@"3")
    });
}

-(void)printLog {
    NSLog(@"2")
}
複製代碼

dispatch_barrier_async()

利用GCD實現多讀單寫
@interface UserCenter()
{
    dispatch_queue_t concurrent_queue;
    NSMutableDictionary *userCenterDic;
}
@end

@implementation UserCenter 

- (id)init 
{
    self = [super init];
    if (self) {
        concurrent_queue = dispatch_queue_create("read_write_queue", DISPATCH_QUEUE_CONCURRENT);
        userCenterDic = [NSMutableDictionary dictionary];
    }
    
    return self;
}

- (id)objectForKey:(NSString *)key 
{
    __block id obj;
    dispatch_sync(concurrent_queue, ^{
        obj = [userCeneterDic objectForKey:key];
    });
    
    return obj;
}

- (void)setObject:(id)obj forKey:(NSString *)key 
{
    dispatch_barrier_async(concurrent_queue, ^{
        [userCenterDic setObject:obj, forKey:key];
    });
}

@end
複製代碼

dispatch_group_async()

@interface GroupObject()
{
    dispatch_queue_t concurrent_queue;
    NSMutableArray <NSURL *> *urlArr;
}
@end

@implementation GroupObject

- (id)init 
{
    self = [super init];
    if (self) {
        concurrent_queue = dispatch_queue_create("concurrent_queue", DISPATCH_QUEUE_CONCURRENT);
        urlArr = [NSMutableArray array];
    }
    
    return self;
}

- (void)handle
{
    dispatch_group_t group = dispatch_group_create();
    
    for (NSURL *url in urlArr) {
        dispatch_group_async(group, concurrent_queue, ^{
            NSLog(@"url is %@", url);
        });
    }
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"完成");
    })
}

@end
複製代碼

二、NSOperation

  • 添加任務依賴
  • 任務執行狀態的控制
  • 最大併發量
任務執行狀態
  • isReady
  • isExecuting
  • isFinished
  • isCancelled
狀態控制

重寫main方法,地城控制變動任務及執行完成狀態,以及任務退出 重寫start方法,自行控制任務狀態多線程

系統經過KVO移除isFinished = YES的NSOperation併發

三、NSThread

四、多線程與鎖

  • NSRecursiveLock
  • NSLock
  • dispatch_semaphore_t
  1. @synchronized 通常在建立單例對象的時候使用
  2. atomic 屬性關鍵字,對被修飾對象進行原子操做
  3. OSSpinLock 自旋鎖 循環等待,不釋放當前資源;輕量級數據訪問
dispatch_semaphore_t
  • dispatch_semaphore_create(1);
  • dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  • dispatch_semaphore_signal(semaphore);
dispatch_semaphore_create()
struct semaphore {
    int value;
    List<thread>;
}

dispatch_semaphore_wait()
{
    S.value = S.value - 1;
    if S.value < 0 then Block(S.List); => 阻塞線程
}

dispatch_semaphore_signal()
{
    S.value = S.value + 1;
    if S.value <= 0 then wakeup(S.List);
}
複製代碼
相關文章
相關標籤/搜索