一、進程和線程
1.什麼是進程
進程是指在系統中正在啟動並執行一個應用程式
每個進程之間是獨立的,每個進程均運行在其專用且受保護的記憶體空間內
比如同時開啟QQ、Xcode,系統就會分別啟動2個進程
通過“活動監視器”可以查看Mac系統中所開啟的進程
2.什麼是線程
1個進程要想執行任務,必須得有線程(每1個進程至少要有1條線程)
線程是進程的基本執行單元,一個進程(程式)的所有任務都線上程中執行
比如使用酷狗播放音樂、使用迅雷下載電影,都需要線上程中執行
3.線程的串列
1個線程中任務的執行是串列的
如果要在1個線程中執行多個任務,那麼只能一個一個地按順序執行這些任務
也就是說,在同一時間內,1個線程只能執行1個任務
比如在1個線程中下載3個檔案(分別是檔案A、檔案B、檔案C)
二、多線程
1.什麼是多線程
1個進程中可以開啟多條線程,每條線程可以並行(同時)執行不同的任務
進程 ->車間,線程->車間工人
多線程技術可以提高程式的執行效率
比如同時開啟3條線程分別下載3個檔案(分別是檔案A、檔案B、檔案C)
2.多線程的原理
同一時間,CPU只能處理1條線程,只有1條線程在工作(執行)
多線程並發(同時)執行,其實是CPU快速地在多條線程之間調度(切換)
如果CPU調度線程的時間足夠快,就造成了多線程並發執行的假象
思考:如果線程非常非常多,會發生什麼情況?
CPU會在N多線程之間調度,CPU會累死,消耗大量的CPU資源
每條線程被調度執行的頻次會降低(線程的執行效率降低)
3.多線程的優缺點
多線程的優點
能適當提高程式的執行效率
能適當提高資源使用率(CPU、記憶體利用率)
多線程的缺點
開啟線程需要佔用一定的記憶體空間(預設情況下,主線程佔用1M,子線程佔用512KB),如果開啟大量的線程,會佔用大量的記憶體空間,降低程式的效能
線程越多,CPU在調度線程上的開銷就越大
程式設計更加複雜:比如線程之間的通訊、多線程的資料共用
4.多線程在iOS開發中的應用
主線程:一個iOS程式運行後,預設會開啟1條線程,稱為“主線程”或“UI線程”
主線程的主要作用
顯示\重新整理UI介面
處理UI事件(比如點擊事件、滾動事件、拖拽事件等)
主線程的使用注意:別將比較耗時的操作放到主線程中。
耗時操作會卡住主線程,嚴重影響UI的流暢度,給使用者一種“卡”的壞體驗
5.程式碼範例
複製代碼 代碼如下:
//
// YYViewController.m
// 01-阻塞主線程
//
// Created by apple on 14-6-23.
// Copyright (c) 2014年 itcase. All rights reserved.
//
#import "YYViewController.h"
@interface YYViewController ()
- (IBAction)btnClick;
@end
複製代碼 代碼如下:
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
//按鈕的點擊事件
- (IBAction)btnClick {
//1.擷取當前線程
NSThread *current=[NSThread currentThread];
//2.使用for迴圈執行一些耗時操作
for (int i=0; i<10000; i++) {
//3.輸出線程
NSLog(@"btnClick---%d---%@",i,current);
}
}
@end
執行效果:
說明:當點擊執行的時候,textView點擊無響應。
執行分析:等待主線程串列執行。
開啟子線程。
三、建立和啟動線程簡單說明
一個NSThread對象就代表一條線程
建立、啟動線程
(1)
複製代碼 代碼如下:
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
[thread start];
// 線程一啟動,就會線上程thread中執行self的run方法
主線程相關用法
複製代碼 代碼如下:
+ (NSThread *)mainThread; // 獲得主線程
- (BOOL)isMainThread; // 是否為主線程
+ (BOOL)isMainThread; // 是否為主線程
其他用法
獲得當前線程
複製代碼 代碼如下:
NSThread *current = [NSThread currentThread];
線程的調度優先順序:調度優先順序的取值範圍是0.0 ~ 1.0,預設0.5,值越大,優先順序越高
複製代碼 代碼如下:
+ (double)threadPriority;
+ (BOOL)setThreadPriority:(double)p;
設定線程的名字
複製代碼 代碼如下:
- (void)setName:(NSString *)n;
- (NSString *)name;
其他建立線程的方式
(2)建立線程後自動啟動線程 [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
(3)隱式建立並啟動線程 [self performSelectorInBackground:@selector(run) withObject:nil];
上述2種建立線程方式的優缺點
優點:簡單快捷
缺點:無法對線程進行更詳細的設定
四、多線程程式碼範例
1.使用古老的方式建立
複製代碼 代碼如下:
//
// YYViewController.m
//
//
// Created by apple on 14-6-23.
// Copyright (c) 2014年 itcase. All rights reserved.
//
#import "YYViewController.h"
#import <pthread.h>
@interface YYViewController ()
- (IBAction)btnClick;
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
//按鈕的點擊事件
- (IBAction)btnClick {
//1.擷取當前線程
NSThread *current=[NSThread currentThread];
//主線程
NSLog(@"btnClick----%@",current);
//2.使用for迴圈執行一些耗時操作
pthread_t thread;
pthread_create(&thread, NULL, run, NULL);
}
//c語言函數
void *run(void *data)
{
//擷取當前線程,是新建立出來的線程
NSThread *current=[NSThread currentThread];
for (int i=0; i<10000; i++) {
NSLog(@"btnClick---%d---%@",i,current);
}
return NULL;
}
//多個線程,點擊按鈕執行按鈕調用方法的時候,主線程沒有被阻塞
@end
實現效果:
列印結果:
2.使用NSThread建立線程
複製代碼 代碼如下:
//
// YYViewController.m
//
//
// Created by apple on 14-6-23.
// Copyright (c) 2014年 itcase. All rights reserved.
//
#import "YYViewController.h"
#import <pthread.h>
@interface YYViewController ()
- (IBAction)btnClick;
@end
複製代碼 代碼如下:
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
//按鈕的點擊事件
- (IBAction)btnClick {
//1.擷取當前線程
NSThread *current=[NSThread currentThread];
//主線程
NSLog(@"btnClick----%@",current);
//擷取主線程的另外一種方式
NSThread *main=[NSThread mainThread];
NSLog(@"主線程-------%@",main);
//2.執行一些耗時操作
[self creatNSThread];
// [self creatNSThread2];
// [self creatNSThread3];
}
/**
* NSThread建立線程方式1
* 1> 先建立初始化線程
* 2> start開啟線程
*/
-(void)creatNSThread
{
NSThread *thread=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"線程A"];
//為線程設定一個名稱
thread.name=@"線程A";
//開啟線程
[thread start];
NSThread *thread2=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"線程B"];
//為線程設定一個名稱
thread2.name=@"線程B";
//開啟線程
[thread2 start];
}
/**
* NSThread建立線程方式2
*建立完線程直接(自動)啟動
*/
-(void)creatNSThread2
{
// NSThread *thread=[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"建立完線程直接(自動)啟動"];
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"建立完線程直接(自動)啟動"];
}
/**
* NSThread建立線程方式3
* 隱式建立線程, 並且直接(自動)啟動
*/
-(void)creatNSThread3
{
//在後台線程中執行===在子線程中執行
[self performSelectorInBackground:@selector(run:) withObject:@"隱式建立"];
}
-(void)run:(NSString *)str
{
//擷取當前線程
NSThread *current=[NSThread currentThread];
//列印輸出
for (int i=0; i<10; i++) {
NSLog(@"run---%@---%@",current,str);
}
}
@end
調用線程1,列印結果為:
調用線程2
調用線程3