iOS面试笔试题

iOS多线程NSThread,GCD ,NSOperation

2016-07-03  本文已影响130人  离离离离

NSThread:

这套方案是经过苹果封装后的,并且完全面向对象的。所以你可以直接操控线程对象,非常直观和方便。但是,它的生命周期还是需要我们手动管理,所以这套方案也是偶尔用用,比如 [NSThread currentThread],它可以获取当前线程类,你就可以知道当前线程的各种属性,用于调试十分方便。下面来看看它的一些用法。

// 创建 
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:nil]; 
// 启动
[thread start];
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:nil];
[self performSelectorInBackground:@selector(run:) withObject:nil];

GCD

Grand Central Dispatch,它是苹果为多核的并行运算提出的解决方案,所以会自动合理地利用更多的CPU内核(比如双核、四核),最重要的是它会自动管理线程的生命周期(创建线程、调度任务、销毁线程),完全不需要我们管理,我们只需要告诉干什么就行。同时它使用的也是 c语言,不过由于使用了 Block(Swift里叫做闭包),使得使用起来更加方便,而且灵活。

NSOperation和NSOperationQueue

添加任务
//1.创建NSInvocationOperation对象 
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil]; //2.开始执行
 [operation start];
//1.创建NSBlockOperation对象 NSBlockOperation
 *operation = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"%@", [NSThread currentThread]); }]; 
//2.开始任务 
operation start];

线程同步

  1. 线程同步就是为了防止多个线程抢夺同一个资源造成的数据安全问题,所采取的一种措施
    • 互斥锁 :给需要同步的代码块加一个互斥锁,就可以保证每次只有一个线程访问此代码块。
@synchronized(self) { //需要执行的代码块}
 - **同步执行** :我们可以使用多线程的知识,把多个线程都要执行此段代码添加到同一个串行队列,这样就实现了线程同步的概念。当然这里可以使用 GCD和 NSOperation 两种方案,我都写出来。
//GCD 
//需要一个全局变量queue,要让所有线程的这个操作都加到一个queue中 
dispatch_sync(queue, ^{ 
NSInteger ticket = lastTicket;
 [NSThread sleepForTimeInterval:0.1];
 NSLog(@"%ld - %@",ticket, [NSThread currentThread]);
 ticket -= 1; lastTicket = ticket; 
}); 
//NSOperation & NSOperationQueue 
//重点:1. 全局的 NSOperationQueue, 所有的操作添加到同一个queue中 
// 2. 设置 queue 的 maxConcurrentOperationCount 为 1 
// 3. 如果后续操作需要Block中的结果,就需要调用每个操作的waitUntilFinished,阻塞当前线程,一直等到当前操作完成,才允许执行后面的。waitUntilFinished 要在添加到队列之后! NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ 
NSInteger ticket = lastTicket; 
[NSThread sleepForTimeInterval:1]; 
NSLog(@"%ld - %@",ticket, [NSThread currentThread]); 
ticket -= 1; lastTicket = ticket; }];
 [queue addOperation:operation];
 [operation waitUntilFinished]; //后续要做的事

延迟执行

延迟执行就是延时一段时间再执行某段代码。下面说一些常用方法。
perform

// 3秒后自动调用self的run:方法,并且传递参数:@"abc"
 [self performSelector:@selector(run:) withObject:@"abc" afterDelay:3];

GCD

// 创建队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 设置延时,单位秒
double delay = 3; 
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), queue, ^{ 
// 3秒后需要执行的任务
});

NSTimer

[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(run:) userInfo:@"abc" repeats:NO];

单例模式

@interface Tool : NSObject <NSCopying>
+(instancetype)sharedTool;
@end
@implementation Toolstatic id _instance;
+(instancetype)sharedTool
 { static dispatch_once_t onceToken; 
  dispatch_once(&onceToken, ^{
 _instance = [[Tool alloc] init]; 
}); 
return _instance;
}
@end

从其他线程回到主线程的方法

NSThread

//Objective-C
[self performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:NO];

GCD

dispatch_async(dispatch_get_main_queue(), ^{
});

NSOperationQueue

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
}];

转载于http://www.jianshu.com/p/0b0d9b1f1f19

上一篇下一篇

猜你喜欢

热点阅读