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];
    }

  • 相关阅读:
    Windows Server 2008上安装 Windows SharePoint Services 3.0
    自定义Unity 容器的扩展 Unity Application Block Event Broker
    .NET Migration工具
    ASP.NET 应用程序的扩展策略[MSDN 杂志]
    命令行解析的规则以及Command Line Parser Library
    Visual Studio 2008 SP1和.NET FX 3.5 SP1发布了
    Entity Framework samples For RTM
    PowerShell的开源实现
    Enterprise Library 4.0缓存应用程序块
    Microsoft SQL Server Community & Samples
  • 原文地址:https://www.cnblogs.com/wangshengl9263/p/3053379.html
Copyright © 2011-2022 走看看