1.進(jìn)程的基本概念
- 每一個(gè)進(jìn)程都是一個(gè)應(yīng)用程序,都有獨(dú)立的內(nèi)存空間,一般來說一個(gè)應(yīng)用程序存在一個(gè)進(jìn)程,但也存在多個(gè)進(jìn)程的情況.
- 同一個(gè)進(jìn)程中的線程共享內(nèi)存中的內(nèi)存和資源.
2.多線程的基本概念
- 每一個(gè)程序都有一個(gè)主線程,程序啟動時(shí)創(chuàng)建(調(diào)用main來啟動)
- 主線程的生命周期是和應(yīng)用程序綁定的,程序退出(結(jié)束)時(shí),主線程也就停止了
- 多線程技術(shù)表示,一個(gè)應(yīng)用程序有多個(gè)線程,使用多線程能增強(qiáng)CPU的使用率,防止主線程堵塞.
- 任何有可能堵塞主線程的任務(wù)不要再主線程執(zhí)行(訪問網(wǎng)絡(luò))
廢話不多說上代碼
//在AppDelegate.m建一個(gè)方法,
- (void)mutableThread:(NSString *)t{
for (int i = 0; i < 200; i ++) {
NSLog(@"-多線程-1-%d",i);
}
//在didFinishLaunching里(其余創(chuàng)建多線程也在這里)
for (int i = 0; i < 200; i ++) {
NSLog(@"-主線程-%d",i);
}
NSTheadd:第一種
//創(chuàng)建多線程對象
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(mutableThread:) object:@"test"];
//開始運(yùn)行多線程
[thread start];
NSTheadd:第二種
[NSThread detachNewThreadSelector:@selector(mutableThread:) toTarget:self withObject:@"test"];
NSTheadd:第三種
[self performSelectorInBackground:@selector(mutableThread:) withObject:nil];
NSTheadd:第四種
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operationQueue addOperationWithBlock:^{
// 里面運(yùn)行多線程的方法
for (int i = 0; i < 200; i ++) {
NSLog(@"-多線程-%d",i);
}
}];
NSOperationQueue
//創(chuàng)建一個(gè)線程隊(duì)列
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
//設(shè)置線程并發(fā)數(shù)的個(gè)數(shù)
operationQueue.maxConcurrentOperationCount = 1;
//創(chuàng)建一個(gè)線程操作對象
NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(mutableThread:) object:nil];
NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(mutableThread2:) object:nil];
//設(shè)置線程數(shù)優(yōu)先級
operation1.queuePriority = NSOperationQueuePriorityHigh;
NSInvocationOperation *operation3 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(mutableThread3:) object:nil];
[operationQueue addOperation:operation1];
[operationQueue addOperation:operation2];
[operationQueue addOperation:operation3];
- (void)mutableThread:(NSString *)t{
for (int i = 0; i < 200; i ++) {
NSLog(@"-多線程-1-%d",i);
}
// 跳到主線程
[self performSelectorOnMainThread:@selector(mainThread) withObject:nil waitUntilDone:YES];
}
- (void)mutableThread2:(NSString *)t{
for (int i = 0; i < 100; i ++) {
NSLog(@"-多線程-2-%d",i);
}
}
- (void)mutableThread3:(NSString *)t{
for (int i = 0; i < 100; i ++) {
NSLog(@"-多線程-3-%d",i);
}
}
GCD
dispatch_queue_t queue = dispatch_queue_create("test", NULL);
dispatch_async(queue, ^{
for (int i = 0; i < 200; i ++) {
NSLog(@"-多線程-%d",i);
}
});
BOOL isMuliti = [NSThread isMultiThreaded];
if (isMuliti) {
NSLog(@"多線程");
}
dispatch_sync(dispatch_get_main_queue(), ^{
BOOL isMain = [NSThread isMainThread];
if (isMain) {
NSLog(@"主線程");
}
});
//次線程還是主線程
dispatch_sync(dispatch_get_main_queue(), ^{
BOOL isMain = [NSThread isMainThread];
if (isMain) {
NSLog(@"主線程");
}
});
- (void)mainThread {
BOOL isMain = [NSThread isMainThread];
if (isMain) {
NSLog(@">>>>>>>>>>>>>>>>>>>>3主線程");
} else {
NSLog(@"其他線程");
}
}
3.三種多線程技術(shù)的對比
?NSThread:–優(yōu)點(diǎn):NSThread 比其他兩個(gè)輕量級,使用簡單–缺點(diǎn):需要自己管理線程的生命周期是牢、線程同步僵井、加鎖、睡眠以及喚醒等驳棱。線程同步對數(shù)據(jù)的加鎖會有一定的系統(tǒng)開銷
?NSOperation:–不需要關(guān)心線程管理批什,數(shù)據(jù)同步的事情,可以把精力放在自己需要執(zhí)行的操作上–NSOperation是面向?qū)ο蟮?br> ?GCD:–Grand Central Dispatch是由蘋果開發(fā)的一個(gè)多核編程的解決方案社搅。iOS4.0+才能使用驻债,是替代NSThread, NSOperation的高效和強(qiáng)大的技術(shù)–GCD是基于C語言的
未經(jīng)博主允許,不得轉(zhuǎn)載!
#好了各位晚安!哈哈哈哈哈,