iOS 多线程-NSOperation&NSOperationQueue

//
//  ViewController.m
//  NSOperation&NSOperationQueue
//
//  Created by BMW on 2018/4/3.
//  Copyright © 2018年 tsouTSoutsou. All rights reserved.
//

#import "ViewController.h"
#import "BMWOperation.h"
@interface ViewController ()
//余票
@property (nonatomic, assign) NSUInteger ticketSurplusCount;

@property (nonatomic, strong) NSLock *lock;
@end

@implementation ViewController

//通过 使用子类 NSInvocationOperation 封装操作
- (void)useInvocationOperation{
    //创建NSInvocationOperation
    NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(task1) object:nil];
    //调用start开始执行操作
    [op start];
}
- (void)task1{
    for (int i = 0; i < 2; i++) {
        [NSThread sleepForTimeInterval:2];
        NSLog(@"1------%@",[NSThread currentThread]);
    }
}

//通过 使用子类 NSBlockOperation 封装操作
- (void)useBlockOperation{
    //创建NSBlockOperation对象
    NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1------%@",[NSThread currentThread]);
        }
    }];
    //调用start 开始执行操作
    [op start];
}

//NSBlockOperation添加额外的操作 addExecutionBlock
- (void)useBlockOperationAndAddExecutionBlock{
    //先创建NSBlockOperation
    NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1------%@",[NSThread currentThread]);
        }
    }];
    
    [op addExecutionBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"2------%@",[NSThread currentThread]);
        }
        }];
    
    [op addExecutionBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"3------%@",[NSThread currentThread]);
        }
    }];
    
    [op addExecutionBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"4------%@",[NSThread currentThread]);
        }
    }];
    
    [op addExecutionBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"5------%@",[NSThread currentThread]);
        }
    }];
    
    [op addExecutionBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"6------%@",[NSThread currentThread]);
        }
    }];
    
    [op addExecutionBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"7------%@",[NSThread currentThread]);
        }
    }];
    //开始执行操作
    [op start];
}

//使用自定义继承 NSOperation 的子类
- (void)customOperation{
    BMWOperation *op = [[BMWOperation alloc] init];
    //调用start开始执行操作
    [op start];
}

//将操作添加到队列中
- (void)addOperationToQueue{
    //获取主队列
//    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
    
    //创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    //创建操作一
    NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(task1) object:nil];
    //创建操作二
    NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(task2) object:nil];
    //创建操作三
    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
        for (int i =  0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"3------%@",[NSThread currentThread]);
        }
    }];
    //给操作三 添加额外 操作
    [op3 addExecutionBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"4------%@",[NSThread currentThread]);
        }
    }];
    
    //添加所有操作到队列中
    [queue addOperation:op1];
    [queue addOperation:op2];
    [queue addOperation:op3];
}
- (void)task2{
    for (int i = 0; i < 2; i++) {
        [NSThread sleepForTimeInterval:2];
        NSLog(@"2------%@",[NSThread currentThread]);
    }
}

//无需创建操作 在block中直接添加操作 直接将包含操作的block添加到队列中
- (void)addOperationWithBlockToQueue{
    //创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    //使用addOperationBlock 添加操作到队列中
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1------%@",[NSThread currentThread]);
        }
    }];
    
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"2------%@",[NSThread currentThread]);
        }
    }];
    
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"3------%@",[NSThread currentThread]);
        }
    }];
    
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"4------%@",[NSThread currentThread]);
        }
    }];
    
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"5------%@",[NSThread currentThread]);
        }
    }];
    
}

//设置最大并发操作数
- (void)setMaxConcurrentOperationCount{
    //创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    //设置最大病并发书
//    queue.maxConcurrentOperationCount = 1;
//    queue.maxConcurrentOperationCount = 2;
    queue.maxConcurrentOperationCount = 8;
    
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1------%@",[NSThread currentThread]);
        }
    }];
    
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"2------%@",[NSThread currentThread]);
        }
    }];
    
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"3------%@",[NSThread currentThread]);
        }
    }];
    
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"4------%@",[NSThread currentThread]);
        }
    }];
    
}

//操作依赖
- (void)addDependncy{
    //创建队列
    NSOperationQueue *queue =  [[NSOperationQueue alloc] init];
    
    //创建操作一
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1------%@",[NSThread currentThread]);
        }
    }];
    
    //添加操作二
    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"2------%@",[NSThread currentThread]);
        }
    }];
    
    //添加依赖
    [op2 addDependency:op1];//op2 依赖于 op1 则先执行 op1 再执行 op2
    
    //添加操作到队列中
    [queue addOperation:op2];
    [queue addOperation:op1];
    
}

//线程间通信
- (void)communication{
    //创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    //添加操作
    [queue addOperationWithBlock:^{
        
        for (int i = 0; i < 50; i++) {
            [NSThread sleepForTimeInterval:0.5];
            NSLog(@"1------%@",[NSThread currentThread]);
        }
//        NSLog(@"1------%@",[NSThread currentThread]);
//        [NSThread sleepForTimeInterval:2];
//        NSLog(@"2------%@",[NSThread currentThread]);
        
        
    }];
    //回到主线程
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        //进行一些用户交互操作 如UI刷新等
        //        [NSThread sleepForTimeInterval:2];
        for (int i = 0; i < 50; i++) {
            [NSThread sleepForTimeInterval:0.5];
            NSLog(@"2------%@",[NSThread currentThread]);
        }
        //        NSLog(@"3------%@",[NSThread currentThread]);
        //        [NSThread sleepForTimeInterval:2];
        //        NSLog(@"4------%@",[NSThread currentThread]);
    }];
}

//线程同步 和线程安全

/**
 非线程安全 不使用NSLock
 初始化火车票数量 售票窗口(非线程安全)并开始卖票
 */
- (void)initTicketStatusNotSafe{
    NSLog(@"currentThread------%@",[NSThread currentThread]);
    
    //初始化最大票数
    self.ticketSurplusCount = 50;
    
    //初始化北京售票窗口(queue1)
    NSOperationQueue *queue1 = [[NSOperationQueue alloc] init];
    //设置最大并发数 每次售卖一张票
    queue1.maxConcurrentOperationCount = 1;
    
    //初始化上海售票窗口(queue2)
    NSOperationQueue *queue2 = [[NSOperationQueue alloc] init];
    //设置最大并发数
    queue2.maxConcurrentOperationCount = 1;
    
    //创建操作一
    //初始化弱引用
    __weak typeof(self) weakSelf = self;
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        [weakSelf saleTicketNotSafe];
    }];
    
    //创建操作二
    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        [weakSelf saleTicketNotSafe];
    }];
    
    //将操作添加到队列
    [queue1 addOperation:op1];
    [queue2 addOperation:op2];
}
/**
 售卖火车票(非线程安全)
 */
- (void)saleTicketNotSafe{
    while (1) {
        if (self.ticketSurplusCount > 0) {
            self.ticketSurplusCount --;
            //打印剩余票数 以及当前售票窗口(当前线程)
            NSLog(@"%@",[NSString stringWithFormat:@"剩余票数:%ld 当前窗口%@",self.ticketSurplusCount, [NSThread currentThread]]);
            [NSThread sleepForTimeInterval:0.2];
        }else{
            NSLog(@"票已售完");
            //跳出当前方法
            break;
        }
    }
}

/**
 
 线程安全的售票方式 使用NSLock
 
 */
- (void)initTicketStatusSafe{
    //打印当前线程
    NSLog(@"currentThread------%@",[NSThread currentThread]);
    
    //初始化余票数
    self.ticketSurplusCount = 50;
    
    //初始化 NSLock
    self.lock = [[NSLock alloc] init];
    
    //初始化北京售票窗口(queue1)
    NSOperationQueue *queue1 = [[NSOperationQueue alloc] init];
    //设置最大并发数
    queue1.maxConcurrentOperationCount = 1;
    
    //初始化上海售票窗口
    NSOperationQueue *queue2 = [[NSOperationQueue alloc] init];
    //设置最大并发数
    queue2.maxConcurrentOperationCount = 1;
    
    //初始化操作一
    //初始化弱引用
    __weak typeof(self) weakSelf = self;
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        [weakSelf saleTicketSafe];
    }];
    
    //初始化操作二
    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        [weakSelf saleTicketSafe];
    }];
    
    //将操作添加到队列中
    [queue1 addOperation:op1];
    [queue2 addOperation:op2];
}

/**
 
 开始售票(线程安全)
 
 */
- (void)saleTicketSafe{
    while (1) {
        
        //加锁
        [self.lock lock];
        
        if (self.ticketSurplusCount > 0) {
            self.ticketSurplusCount --;
            NSLog(@"%@",[NSString stringWithFormat:@"剩余票数:%ld 当前窗口%@",self.ticketSurplusCount, [NSThread currentThread]]);
        }else{
            NSLog(@"票已经售完");
            [self.lock unlock];
            break;
        }
        [self.lock unlock];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
//    [self useInvocationOperation];
//    [NSThread detachNewThreadSelector:@selector(useInvocationOperation) toTarget:self withObject:nil];//开新线程 调用方法
//    [self useBlockOperation];
//    [NSThread detachNewThreadSelector:@selector(useBlockOperation) toTarget:self withObject:nil];
//    [self useBlockOperationAndAddExecutionBlock];
//    [self customOperation];
//    [self addOperationToQueue];
//    [self addOperationWithBlockToQueue];
//    [self setMaxConcurrentOperationCount];
//    [self addDependncy];
//    [self communication];
    [NSThread detachNewThreadSelector:@selector(communication) toTarget:self withObject:nil];
//    [self initTicketStatusNotSafe];
//    [self initTicketStatusSafe];
    //测试程序走完
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, self.view.frame.size.width - 200, 50)];
    label.text = @"程序已经启动啦!";
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:label];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

github地址

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,607评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,047评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,496评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,405评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,400评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,479评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,883评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,535评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,743评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,544评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,612评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,309评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,881评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,891评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,136评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,783评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,316评论 2 342

推荐阅读更多精彩内容