IOS线程学习(一)

时间:2023-12-25 17:40:25

1.NSThread 

官方的描述

An NSThread object controls a thread of execution. Use this class when you want to have an Objective-C method run in its own thread of execution. Threads are especially useful when you need to perform a lengthy task, but don’t want it to block the execution of the rest of the application.

NSThread能控制一个线程的执行, 当你想在自己的线程执行OC方法时请用此类。对于执行较长的任务时这是也很有用的,不会堵住程序里面剩下需要执行的任务。

 NSThread *thread = [[NSThread alloc]initWithTarget:self selector:NSSelectorFromString(@"myThread:") object:nil];
//启动线程
[thread start];
//停止线程
//if (![thread isCancelled]) {
//[thread cancel];

//    }

 -(void)myThread:(id)sender{
NSLog(@"%@" , sender);
@synchronized(self){
while (true) {
[NSThread sleepForTimeInterval:];
static int i = ;
NSLog(@"%i" , i++);
if (i == ) {
[NSThread exit];
}
}
}
}

结果:只打印到3时线程就终止了

IOS线程学习(一)

也可以用这个方法启动一个线程,但是不能是默认的Thread配置

[NSThread detachNewThreadSelector:NSSelectorFromString(@"myThread:") toTarget:self withObject:@"myThread"];

也等同于,这个方法在NSObject中被定义,只要是继承NSObject都可以这样用

[self performSelectorInBackground:NSSelectorFromString(@"myThread:") withObject:@"myThread"];

 2.NSOperation

目前我的理解就是一个封装操作某操作的,然后调用其start方法,就在主线程执行!!!

如果不在主线程执行可以创建一个NSOperationQueue,然后将操作加入到其中执行

其有两个子类NSBlockOperation和NSInvocationOperation

NSBlockOperation

The NSBlockOperation class is a concrete subclass of NSOperation that manages the concurrent execution of one or more blocks. You can use this object to execute several blocks at once without having to create separate operation objects for each. When executing more than one block, the operation itself is considered finished only when all blocks have finished executing.

可见,NSBlockOperation是管理多个Block块的,而且只有所有的Block都执行完了才会变成finished状态;

    NSLog(@"%@ mainT = " ,[NSThread currentThread]);

    NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{
for (int i = ; i<; i++) {
[NSThread sleepForTimeInterval:];
NSLog(@"block1>>>%i thread = %@" , i , [NSThread currentThread]);
}
}]; [blockOp addExecutionBlock:^{
for (int i = ; i<; i++) {
[NSThread sleepForTimeInterval:];
NSLog(@"block2>>>%i thread = %@" , i , [NSThread currentThread]);
}
}]; [blockOp start]; NSLog(@"到这了");

运行结果:一个Block就在主线程,多个就会并行执行其他block

IOS线程学习(一)

NSInvocationOperation

The NSInvocationOperation class is a concrete subclass of NSOperation that manages the execution of a single encapsulated task specified as an invocation. You can use this class to initiate an operation that consists of invoking a selector on a specified object. This class implements a non-concurrent operation.

可见,只能通过Action-Target模式加入一个操作

      NSLog(@"%@ mainThread = " ,[NSThread currentThread]);
NSInvocationOperation *invocationOperation1= [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(myOperation:) object:@"invocationOperation1"]; [invocationOperation1 start]; NSLog(@"到这了"); -(void)myOperation:(id)sender{
static int i = ;
while (i<) {
[NSThread sleepForTimeInterval:];
[NSThread isMainThread];
NSLog(@"我是线程%@ %i", [NSThread currentThread] , i++);
}
}

运行结果:

-- ::23.038 MYThread[:] <NSThread: 0x7ffcba50c190>{number = , name = main} mainThread =
-- ::24.043 MYThread[:] 我是线程<NSThread: 0x7ffcba50c190>{number = , name = main}
-- ::25.046 MYThread[:] 我是线程<NSThread: 0x7ffcba50c190>{number = , name = main}
-- ::26.051 MYThread[:] 我是线程<NSThread: 0x7ffcba50c190>{number = , name = main}
-- ::27.053 MYThread[:] 我是线程<NSThread: 0x7ffcba50c190>{number = , name = main}
-- ::27.054 MYThread[:] 到这了

 3.NSOperationQueue

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

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

    [queue addOperation:op];

    NSLog(@"到这了!");

结果:

2016-03-17 09:17:22.710 ViewAnim[580:12599] 到这了!
2016-03-17 09:17:23.779 ViewAnim[580:12871] 111111
2016-03-17 09:17:24.853 ViewAnim[580:12871] 111111
2016-03-17 09:17:25.926 ViewAnim[580:12871] 111111
2016-03-17 09:17:27.001 ViewAnim[580:12871] 111111
2016-03-17 09:17:28.070 ViewAnim[580:12871] 111111
2016-03-17 09:17:29.143 ViewAnim[580:12871] 111111
。。。。。