iOS开发之多线程重点总结

//1.异步并发的队列:是用得比较多的


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // dispatch_sync : 同步,不具备开启线程的能力 // dispatch_async : 异步,具备开启线程的能力 // 并发队列 :多个任务可以同时执行 // 串行队列 :一个任务执行完后,再执行下一个任务 // 获得全局的并发队列 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 将 任务 添加 全局队列 中去 异步 执行 dispatch_async(queue, ^{ NSLog(@"-----下载图片1---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"-----下载图片2---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"-----下载图片3---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"-----下载图片4---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"-----下载图片5---%@", [NSThread currentThread]); }); }

//2.异步下载图片 然后回到主线程加载图片 刷新UI

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperationWithBlock:^{
        // 1.异步下载图片
        NSURL *url = [NSURL URLWithString:@"http://d.hiphotos.baidu.com/image/pic/item/37d3d539b6003af3290eaf5d362ac65c1038b652.jpg"];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        
        // 2.回到主线程,显示图片
//        [self performSelectorOnMainThread:<#(SEL)#> withObject:<#(id)#> waitUntilDone:<#(BOOL)#>];
//        dispatch_async(dispatch_get_main_queue(), ^{
//            
//        });
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.imageView.image = image;
        }];
    }];
}
 
  

//3.给线程任务间添加依赖 

- (void)dependency
{
    /**
     假设有A、B、C三个操作,要求:
     1. 3个操作都异步执行
     2. 操作C依赖于操作B
     3. 操作B依赖于操作A
     */
    
    // 1.创建一个队列(非主队列)
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    // 2.创建3个操作
    NSBlockOperation *operationA = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"A1---%@", [NSThread currentThread]);
    }];
    
    NSBlockOperation *operationB = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"B---%@", [NSThread currentThread]);
    }];
    NSBlockOperation *operationC = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"C---%@", [NSThread currentThread]);
    }];
    
    // 设置依赖
    [operationB addDependency:operationA];
    [operationC addDependency:operationB];
    
    // 3.添加操作到队列中(自动异步执行任务)
    [queue addOperation:operationC];
    [queue addOperation:operationA];
    [queue addOperation:operationB];
}

//4.最大并发数

    queue.maxConcurrentOperationCount = 3;

//5.一般在内存警告中取消线程操作

    [queue cancelAllOperations];//不可恢复的操作

  [queue setSuspended:YES]; // 暂停队列中的所有任务

  [queue setSuspended:NO]; // 恢复队列中的所有任务

//6.线程安全 :需要加锁 @synchronize(self){}

首先创建多个线程:执行相同的任务  (实现卖票的情景 需要给票数赋初值)

 
  
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [NSThread detachNewThreadSelector:@selector(sellTicket:) toTarget:self withObject:@"a"];
    [NSThread detachNewThreadSelector:@selector(sellTicket:) toTarget:self withObject:@"b"];
    [NSThread detachNewThreadSelector:@selector(sellTicket:) toTarget:self withObject:@"c"];    
    
}
 
  

然后再任务实现中加锁:

//实现买票的方法  需要加密

- (void)sellTicket:(NSString *)name {
    
    
    while (1) {
        @synchronized(self) {
            if (self.ticketCount > 0) {
                [NSThread sleepForTimeInterval:0.3];
                self.ticketCount--;
                NSLog(@"%@---%d----%@---%D",[NSThread currentThread],[NSThread isMainThread],name,self.ticketCount);

            } else {
                return;
            }
        }
    }
    
}

 












 

你可能感兴趣的:(iOS开发之多线程重点总结)