网络多线程-NSOperation的简单使用
2015-11-27 21:43
399 查看
import "ViewController.h"
#import "XMGOperation.h"
@interface
ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self
customOperation];
}
-(void)customOperation
{
NSLog(@"-----");
//1.封装操作
XMGOperation *op1 = [[XMGOperation
alloc]init];
XMGOperation *op2 = [[XMGOperation
alloc]init];
//2.启动执行
[op1 start];
[op2 start];
}
-(void)invocationOperation
{
//1.封装操作
/*
第一个参数:目标对象SELF
第二个参数:要调用的是哪个方法
第三个参数:方法的参数
*/
NSInvocationOperation *op1 = [[NSInvocationOperation
alloc]initWithTarget:self
selector:@selector(run)
object:nil];
//2.启动执行操作
[op1 start];
NSInvocationOperation *op2 = [[NSInvocationOperation
alloc]initWithTarget:self
selector:@selector(run)
object:nil];
//2.启动执行操作
[op2 start];
NSInvocationOperation *op3 = [[NSInvocationOperation
alloc]initWithTarget:self
selector:@selector(run)
object:nil];
//2.启动执行操作
[op3 start];
}
-(void)blockOperation
{
//1.封装操作
NSBlockOperation *op1 = [NSBlockOperation
blockOperationWithBlock:^{
//主线程
NSLog(@"1---%@",[NSThread
currentThread]);
}];
NSBlockOperation *op2 = [NSBlockOperation
blockOperationWithBlock:^{
NSLog(@"2---%@",[NSThread
currentThread]);
}];
NSBlockOperation *op3 = [NSBlockOperation
blockOperationWithBlock:^{
NSLog(@"3---%@",[NSThread
currentThread]);
}];
//添加额外任务
[op3 addExecutionBlock:^{
//子线程
NSLog(@"4---%@",[NSThread
currentThread]);
}];
[op3 addExecutionBlock:^{
NSLog(@"5---%@",[NSThread
currentThread]);
}];
//2.启动执行操作
[op1 start];
[op2 start];
[op3 start];
}
-(void)run
{
NSLog(@"run---%@",[NSThread
currentThread]);
}
@end
#import "XMGOperation.h"
@interface
ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self
customOperation];
}
-(void)customOperation
{
NSLog(@"-----");
//1.封装操作
XMGOperation *op1 = [[XMGOperation
alloc]init];
XMGOperation *op2 = [[XMGOperation
alloc]init];
//2.启动执行
[op1 start];
[op2 start];
}
-(void)invocationOperation
{
//1.封装操作
/*
第一个参数:目标对象SELF
第二个参数:要调用的是哪个方法
第三个参数:方法的参数
*/
NSInvocationOperation *op1 = [[NSInvocationOperation
alloc]initWithTarget:self
selector:@selector(run)
object:nil];
//2.启动执行操作
[op1 start];
NSInvocationOperation *op2 = [[NSInvocationOperation
alloc]initWithTarget:self
selector:@selector(run)
object:nil];
//2.启动执行操作
[op2 start];
NSInvocationOperation *op3 = [[NSInvocationOperation
alloc]initWithTarget:self
selector:@selector(run)
object:nil];
//2.启动执行操作
[op3 start];
}
-(void)blockOperation
{
//1.封装操作
NSBlockOperation *op1 = [NSBlockOperation
blockOperationWithBlock:^{
//主线程
NSLog(@"1---%@",[NSThread
currentThread]);
}];
NSBlockOperation *op2 = [NSBlockOperation
blockOperationWithBlock:^{
NSLog(@"2---%@",[NSThread
currentThread]);
}];
NSBlockOperation *op3 = [NSBlockOperation
blockOperationWithBlock:^{
NSLog(@"3---%@",[NSThread
currentThread]);
}];
//添加额外任务
[op3 addExecutionBlock:^{
//子线程
NSLog(@"4---%@",[NSThread
currentThread]);
}];
[op3 addExecutionBlock:^{
NSLog(@"5---%@",[NSThread
currentThread]);
}];
//2.启动执行操作
[op1 start];
[op2 start];
[op3 start];
}
-(void)run
{
NSLog(@"run---%@",[NSThread
currentThread]);
}
@end
相关文章推荐
- 网络多线程 -单例模式业余练习代码MRC与ARC通用
- 网络多线程-单例模式ARC业余练习代码
- 网络多线程-GCD队列组
- 网络多线程 - 01-掌握-GCD快速迭代
- 08 SimpleServer
- Tornado源码分析之http服务器篇
- 关闭手机的通讯网络(无法接通)
- NodeJS服务器:一行代码 = 一个的HTTP服务器
- 计算机网络基础笔记
- centos配置网络的问题(ip地址、DNS、主机名)
- Java——多人聊天室(基于TCP的网络编程——Socket)
- 神经网络
- [连载]《C#通讯(串口和网络)框架的设计与实现》- 6.通讯控制器的设计
- socket网络编程
- 关于电脑DNS出错,如何修复和手动设置DNS
- 网络通信加密处理
- 网络部分整理第三部分(XML解析)
- Android使用okhttp下载图片
- iOS 开发笔记-AFNetWorking https SSL认证
- 机器学习--神经网络