zl程序教程

您现在的位置是:首页 >  后端

当前栏目

operation 多线程

多线程 operation
2023-09-27 14:21:34 时间

2.Cocoa Operation 
优点:不需要关心线程管理,数据同步的事情。
Cocoa Operation 相关的类是 NSOperation ,NSOperationQueue。NSOperation是个抽象类,使用它必须用它的子类,可以实现它或者使用它定义好的两个子类:NSInvocationOperation 和 NSBlockOperation。

创建NSOperation子类的对象,把对象添加到NSOperationQueue队列里执行,我们会把我们的执行操作放在NSOperation中main函数中。

1),使用NSBlockOperation

NSOperationQueue *opq=[[ NSOperationQueue alloc]  init];

[opq addOperationWithBlock:^{  ,多线程,,  }];

 

2),使用NSInvocationOperation

NSOperationQueue *opq=[[ NSOperationQueue alloc]  init];

NSInvocationOperation *op1=[[NSInvocationOperation  alloc ]  initWithTarget: self  selector: @selector(thread1)  object: nil ];

[opq   addOperation : op1];