[第2章]多线程:NSOperation的简单使用

一、介绍

特点

  1. 在GCD基础上实现
  2. 更好地控制和处理大量并发操作
  3. 比GCD速度稍慢

1、NSOperation

NSOperation是抽象类,使用方法:

1、使用系统提供的子类;

2、自定义类。

依赖关系:

键值对象:

2、NSOperationQueue

NSOperationQueue,用于调度和管理NSOperation。

二、使用系统类

(并行)NSBlockOperation

代码

// 1、operation1和operation2
let operation1 = NSBlockOperation {
    sleep(1)
    NSLog("operation1: 1")
}

let operation2 = NSBlockOperation {
    sleep(2)
    NSLog("operation2: 1")
}

// 1、在operation2中额外添件一个block(NSOperation类似dispatch_group)
operation2.addExecutionBlock {
    sleep(2)
    NSLog("operation2: 2")
}

// 2、添加依赖关系,和执行完operation2之后的操作(类似dispatch_group_notify)
operation2.addDependency(operation1)
operation2.completionBlock = {
    NSLog("operation2: OK")
}

// 3、创建一个队列,并添加operation到队列中。
let queue = NSOperationQueue()
queue.addOperation(operation1)
queue.addOperation(operation2)

//--------------------------其他------------------------//
// 手动禁止(或允许)新的operation加入队列。
queue.suspended = true
queue.suspended = false

// 设置最大并行运行数:
queue.maxConcurrentOperationCount = 2

结果

operation1执行完,才到operation2,并行执行。

附录

NSInvocationOperation在swift中不能用

自定义类

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-06 05:38:03

[第2章]多线程:NSOperation的简单使用的相关文章

[第2章]多线程:NSThread简介

一.说明 操作的是线程,不是封装好的队列或队列:适用于长时间一直使用的子线程. 二.方法 1.睡眠 // 1.睡到指定时间. class func sleepUntilDate(date: NSDate) // 2.跟sleep()一样(PS:typealias NSTimeInterval = Double) class func sleepForTimeInterval(ti: NSTimeInterval) 2.线程运行Selector NSThread.detachNewThreadSe

[第2章]多线程:GCD的简单使用(2)——调度组

接着上篇 Dispatch Groups 介绍 Grouping blocks allows for aggregate synchronization. Your application can submit multiple blocks and track when they all complete, even though they might run on different queues. This behavior can be helpful when progress can

[第1章]地图:iOS简易导航

一.类介绍 MKDirections 官方文档: 解析: 1.从Apple服务得到导航服务,执行导航需要一个请求类--MKDirectionRequest(下面介绍): 2.每个实例只处理一次导航请求,如果处理多个请求,要创建多个实例: 3.不要在短时间内,请求过多次,否则触发MKErrorLoadingThrottled. // request must not be nil. init(request request: MKDirectionsRequest) // 计算导航路线,在闭包ha

java_第9章:多线程

030901_[第9章:多线程]_认识多线程 030902_[第9章:多线程]_线程常用操作方法 030903_[第9章:多线程]_线程操作范例 030904_[第9章:多线程]_同步与死锁 030905_[第9章:多线程]_线程操作案例--生产者和消费者 030906_[第9章:多线程]_线程生命周期

[Swift中错误]missing argument label 'greeting:' in call

Swift 中出现这个问题:从第二个参数起,自动加上lable func sayHello(name:String? ,greeting:String)->String { let result = greeting + "," + (name ?? "Guest") + "!" return result } var nickname:String? nickname = "yc" //“Goodmorning前面应该

第九章 用多线程来读取epoll模型下的客户端数据

#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <assert.h> #include <stdio.h> #include <unistd.h> #include <errno.h> #include <string.h> #include

MAC OS X socket 1 [a simple example]

Server Code C: // // main.c // unix_socket_very_simple_server // // Created by DMD on 4/7/14. /* Function : Server for unix (OS X MAC) */ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> // unix socket #include <sys/socke

MAC OS X socket 2 [a simple example]

Xcode->New Workspace XCode->New Project: C: [Add to : ?] Server: code // // main.c // s2 // Server // Created by DMD on 4/7/14. #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #includ

图像载入 imshow()[OpenCV 笔记5]

void imshow(const string& winname InputArray mat); winname 窗口表识名称 mat 需要显示的图像.InputArray类型,声明如下 typedef const _InputArray& InputArray; _InputArray定义比较复杂,类里先定义了一个枚举,然后是各类的模版类型和一些方法.遇到InputArray/OutputArray类型,可以把它当作Mat类型处理. 图像大小缩放 如果窗口是用CV_WINDOW_AU