首先分析多線程的使用環(huán)境:
多線程處理包括Core Data的多線程訪問,UI的并行繪制,異步網(wǎng)絡(luò)請求以及一些在運行態(tài)內(nèi)存吃緊的情況下處理大文件的方案等。
其次,分別舉例說明iOS提供的多線程的實現(xiàn)方法
iOS中提供了以下集中中多線程的實現(xiàn)方式
1.NSOBjcet實現(xiàn)
// 最簡單的多線程 執(zhí)行方式
// 參數(shù)1:需要在后臺(子線程)執(zhí)行方法
// 參數(shù)2:給這個方法傳參
[self performSelectorInBackground:@selector(btnUpClicked:)withObject:nil];
2.NSThread實現(xiàn)
// 優(yōu)點:在所有的多線程實現(xiàn)方式中? 最輕量級
//? ? ? 可以按照需求 任意控制thread對象 即:線程的加鎖等操作
// 缺點:控制太繁瑣 不能自己控制線程安全
// NSThread 的一個對象 就代表一個線程
NSThread *thread = [[NSThread alloc]initWithTarget:selfselector:@selector(btnUpClicked:)object:nil];
[thread start];
[thread release];
3.NSOperation實現(xiàn)
// NSOperation代表一個任務(wù) 自己不能實現(xiàn)多線程
// NSOperation是一個抽象類 不能直接使用 需要創(chuàng)建一個子類去編寫實現(xiàn)的內(nèi)容
// 任務(wù)開始 會在當(dāng)前線程執(zhí)行任務(wù)(main方法中的代碼)
MyOperation *operation = [[MyOperation alloc] init];
[operation start];
[operation release];
// 系統(tǒng)已經(jīng)寫好了兩個類 可以直接使用
//? ? NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:<#(id)#> selector:<#(SEL)#> object:<#(id)#>]
NSBlockOperation *block = [NSBlockOperation blockOperationWithBlock:^{
// 填寫你要執(zhí)行的任務(wù)內(nèi)容
}];
@implementation MyOperation
- (void)main
{
// operation在main方法中寫要執(zhí)行的任務(wù)
static int count = 0 ;
for (int i = 0 ; i < 600000000; i ++) {
count ++;
}
NSLog(@"%d", count);
// 判斷當(dāng)前任務(wù)是否為主線程? 0為不是 1為是
BOOL b = [NSThread isMainThread];
NSLog(@"%d", b);
NSThread *thread = [NSThread currentThread];
NSLog(@"當(dāng)前線程為:%@", thread);
}
@end
4.NSOperationQueue
// 任務(wù)隊列(NSOperationQueue) 內(nèi)部管理一些列的線程
MyOperation *op1 = [[MyOperation alloc] init];
MyOperation *op2 = [[MyOperation alloc] init];
MyOperation *op3 = [[MyOperation alloc] init];
MyOperation *op4 = [[MyOperation alloc] init];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
// 設(shè)置最大并發(fā)數(shù)
[queue setMaxConcurrentOperationCount:2];
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperation:op3];
[queue addOperation:op4];
// 優(yōu)點:能夠自己管理 線程安全 能夠合理的安排線程 降低系統(tǒng)的開銷 提高對線程的利用率
// 缺點:可讀性差 寫起來比較繁瑣
5.GCD
// GCD 是基于隊列的 多線程實現(xiàn)方式
// 隊列的類型
// 1.并行: 多個任務(wù)同時執(zhí)行 Concurrent
// 2.串行: 依次執(zhí)行任務(wù)(同一事件只執(zhí)行一個任務(wù)) Serial
// 自己創(chuàng)建一個隊列
// 參數(shù)1:給隊列起個名字
// 參數(shù)2:選擇隊列的類型 DISPATCH_QUEUE_CONCURRENT(串行)
dispatch_queue_attr_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
// 使用隊列
// 參數(shù)1: 在哪個隊列執(zhí)行代碼
// 參數(shù)2: 要執(zhí)行的代碼
dispatch_async(queue, ^{
[self btnUpClicked:nil];
});
高級用法:
// 系統(tǒng)定義了5個隊列
// 1. 一個串行隊列? 主隊列作用 : 管理主線程的相關(guān)事務(wù)
// 獲得主隊列
dispatch_queue_t mainQueue = dispatch_get_main_queue();
// 在主隊列執(zhí)行任務(wù)
//? ? dispatch_async(mainQueue, ^{
//? ? ? ? [self btnUpClicked:nil];
//? ? });
// 2. 4個并行隊列 (并行隊列)
// 參數(shù)1: 選擇隊列的優(yōu)先級
// 參數(shù)2: 給未來使用 一般填0
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//? ? dispatch_async(globalQueue, ^{
//? ? ? ? [self btnUpClicked:nil];
//? ? });
// 高端使用
// 1.全局隊列獲取數(shù)據(jù) 回到主隊列將數(shù)據(jù)加載到視圖上
//? ? dispatch_async(globalQueue, ^{
//
//? ? ? ? // 全局隊列(子線程)獲取數(shù)據(jù)
//? ? ? ? NSString *str = @"htt://www.baidu.com";
//? ? ? ? NSURL *url = [NSURL URLWithString:str];
//
//? ? ? ? NSData *data = [NSData dataWithContentsOfURL:url];
//? ? ? ? UIImage *image = [UIImage imageWithData:data];
//
//? ? ? ? // 返回主線程 加載UI/更新UI
//? ? ? ? dispatch_async(mainQueue, ^{
//
//? ? ? ? ? ? UIImageView *imageView = [[UIImageView alloc] init];
//? ? ? ? ? ? // 給視圖賦值
//? ? ? ? ? ? imageView.image = image;
//
//? ? ? ? });
//? ? });
// 2.一段代碼只執(zhí)行依次
// 大部分的時候 都在單例方法中使用
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"只執(zhí)行依次");
});
// 3.一段代碼執(zhí)行多次 (全局隊列有效)
// 參數(shù)1: 執(zhí)行多少次
// 參數(shù)2: 在哪個隊列有效
// 參數(shù)3: 要執(zhí)行的內(nèi)容
dispatch_apply(4, globalQueue, ^(size_t a) {
NSLog(@"%zu", a);
});
// 4.定時
// 參數(shù)1: 間隔幾秒
// 參數(shù)2: 執(zhí)行的內(nèi)容
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"=====");
});
強烈建議用GCD去嚇唬人。這可不是我說的,是他們總結(jié)的,反正好用就是個,尤其是遇到面試官不是iOS開發(fā)的。。。左拉有扯地拉他回GCD。一句話GCD萬歲~