1.線程的含義
了解線程之前,我們要分清楚程序,進程和線程三者的區別.程序是指由源代碼生成的可執行的應用(例如 QQ.app);一個正在運行的程序可以看做是一個進程,進程擁有獨立運行所需的全部資源(正在運行的 QQ);而線程是指程序中獨立運行的代碼段(接收 QQ 消息的代碼).
2.為什么要使用多線程
當用戶執行某項操作如上傳圖片,主線程會執行此操作.直到上傳結束,主線程才能繼續后面的工作.在這期間,主線程處于"忙碌"狀態,不會對用戶的請求作出任何反應.既然單一線程不能滿足需要,就需要添加線程進行操作.在用戶要進行上述操作是可以啟動一個獨立的線程來專門負責這項操作.這樣一來,兩個線程并行不悖,各司其職.
3.多線程簡單認識
3.1多線程的幾種形式
3.1.1pthread的簡單認識
創建線程指針 ?
pthread_t pthread;
//第一個參數:線程指針//第二個參數:線程屬性//第三個參數:函數指針 用于執行方法//第四個參數:線程傳值 ??
??pthread_create(&pthread, NULL, run, NULL);
3.1.2NSThread
注意事項:每個線程都維護著與自己對應的 NSAutoreleasePool對象,將其放在線程棧的棧頂,當線程結束時,會清空自動釋放池;為保證對象的及時釋放,在多線程方法中需要添加自動釋放池;在應用程序打開時,系統會自動為主線程創建自動釋放池.
3.1.3NSObject
- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg后臺執行
3.1.4NSOperationQuene
操作隊列
目的:是將我們的任務放在一個隊列中執行???
任務:任務執行在主線程還是在子線程全都是由我們的隊列來決定的 ??
加入到隊列 隊列:先加的先執行,后加的后執行,但是執行的時間不一定 可能后執行的比先執行的先執行完
3.1.5GCD(Grand Central Dispatch)是 Apple 開發的多核編程技術
GCD 主要分串行隊列和并行隊
//異步:不在一個線程執行
//同步:在同一個線程執行
//串行:串在一起執行
//并行:一起執行
/*
*異步+并發隊列 具備開辟子線程的能力,并發執行任務
*/
- (void)creatAsyncConcurrent
{
dispatch_queue_tqueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(queue, ^{
NSLog(@"1---------%@",[NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2---------%@",[NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3---------%@",[NSThreadcurrentThread]);
});
}
/*
*同步+并發隊列不具備開辟線程的能力,并發的功能也就沒用了
*/
- (void)creatSyncConcurrent
{
//我們自己創建的并發隊列
//第一個參數:隊列的名字
//第二個參數:類型
//dispatch_queue_t queue = dispatch_queue_create("ddd", DISPATCH_QUEUE_CONCURRENT);
//獲得全局的并發隊列
//第一個參數:為一個優先級,默認的就行
dispatch_queue_tqueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_sync(queue, ^{
NSLog(@"1-----------%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"2-----------%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"3-----------%@",[NSThreadcurrentThread]);
});
}
/*
*異步+串行 具備開辟線程的能力,但是任務是串行的,執行完一個才回去執行下一個
*/
- (void)creatAsyncSerial
{
dispatch_queue_tqueue =dispatch_queue_create("bbb",DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
NSLog(@"1-----------%@",[NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2-----------%@",[NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3-----------%@",[NSThreadcurrentThread]);
});
}
/*
*同步+串行隊列不具備開辟線程的能力,在當前線程完成
*/
- (void)creatSyncSerial
{
//創建串行隊列
dispatch_queue_tqueue =dispatch_queue_create("aaa",DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
NSLog(@"1-----%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"2-----%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"3-----%@",[NSThreadcurrentThread]);
});
}
/*
*異步+主隊列 不開辟線程,就在主線程執行
*/
- (void)creatAsyncMain
{
dispatch_queue_tqueue =dispatch_get_main_queue();
dispatch_async(queue, ^{
NSLog(@"1------------%@",[NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2------------%@",[NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3------------%@",[NSThreadcurrentThread]);
});
}
/*
*同步+主隊列
*/
- (void)creatSyncMain
{
//獲得主隊列(主隊列也是一個串行隊列)
dispatch_queue_tqueue =dispatch_get_main_queue();
//將任務加到隊列中
//第一個參數:隊列
//第二個參數:要執行的任務
dispatch_sync(queue, ^{
NSLog(@"1-----------------%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"2-----------------%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"3-----------------%@",[NSThreadcurrentThread]);
});
}
GCD 常用異步并行隊列.