zoukankan      html  css  js  c++  java
  • 将线程添加到队列

    #pragma mark 简单添加一个操作到队列中
    - (void)exeOperation {
        NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"执行了一个操作:%@", [NSThread currentThread]);
        }];
           
        NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];   
        // 一个NSOperation被添加到队列中会马上执行操作(异步执行)
        [queue addOperation:operation];
    }

    #pragma mark 添加操作到队列中
    - (void)exeOperation3 {
        NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
        
        NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"执行第1个操作:%@", [NSThread currentThread]);
        }];
        
        NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"执行第2个操作:%@", [NSThread currentThread]);
        }];
        
        // 添加依赖
        [operation1 addDependency:operation2];
        
        [queue addOperation:operation1];
        [queue addOperation:operation2];
    }

    #pragma mark 添加操作到队列中
    - (void)exeOperation2 {
        NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
        
        [queue addOperationWithBlock:^{
            NSLog(@"执行了第1个操作:%@", [NSThread currentThread]);
        }];
        
        [queue addOperationWithBlock:^{
            NSLog(@"执行了第2个操作:%@", [NSThread currentThread]);
        }];
        
        [queue addOperationWithBlock:^{
            NSLog(@"执行了第3个操作:%@", [NSThread currentThread]);
        }];
        
        [queue addOperationWithBlock:^{
            NSLog(@"执行了第4个操作:%@", [NSThread currentThread]);
        }];
        
        // 设置最大并发操作数量:1
        // 只允许同时开1条线程
        [queue setMaxConcurrentOperationCount:1];
    }

  • 相关阅读:
    1-Java继承中多态情况特性下变量,方法,静态方法的访问
    3-表约束修改
    2-表结构修改
    持续运维与监控整理
    vscode markdown思维导图插件
    [Python][学习资料]Python学习资料整理
    [高效办公]使用synergy让多台电脑共用一套鼠标键盘
    vim代码片段插件ultisnips的使用
    matplotlib提示RuntimeWarning: Glyph 20998 missing from current font
    vscode使用anaconda的python环境提示“Can't connect to HTTPS URL because the SSL module is not available”
  • 原文地址:https://www.cnblogs.com/wangshengl9263/p/3053379.html
Copyright © 2011-2022 走看看