iOS之[多線程:GCD:Dispatch]

1.dispatch_queue

隊列分爲三種數據庫

1.1 Serial:串行隊列(private_serial_queue)

同時只執行一個任務網絡

當你建立多個Serial queue時,雖然它們各自是同步執行的,但Serial queue與Serial queue之間是併發執行的。併發

原型:app

dispatch_queue_create(
    "queue_name", 
    dispatch_queue_attr_t attr
);

dispatch_queue_attr_t分爲:DISPATCH_QUEUE_SERIAL,DISPATCH_QUEUE_CONCURRENT
1.2 Concurrent:並行隊列(Global Queue)

能夠併發地執行多個任務,可是執行完成的順序是隨機的異步

由整個進程共享async

進程中存在三種全局隊列:高/中(默認)/低 三種優先級隊列函數

原型:ui

dispatch_get_global_queue(優先級,0/nil);

優先級:spa

DISPATCH_QUEUE_PRIORITY_HIGHT
DISPATCH_QUEUE_PRIORITY_DEFAULT
DISPATCH_QUEUE_PRIORITY_LOW
1.3 The Main Queue:串行隊列(public_serial_queue)

與主線程功能相同,提交至main queue的任務會在主線程中執行.net

dispatch_get_main_queue();

2.dispatch_async:異步

爲了不界面在處理耗時的操做時卡死,好比讀取網絡數據,IO,數據庫讀寫等,咱們會在另一個線程中處理這些操做,而後通知主線程更新界面。 注意:不要在dispatch_async中使用dispatch_sync

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue,^{
    //耗時的操做 
    
    dispatch_async(dispatch_get_main_queue, ^{
       //更新界面 
    });
    
});

3.dispatch_group_async

dispatch_group_async能夠實現監聽一組任務是否完成。(頗有用)

配合dispatch_group_t,dispatch_group_notify使用

原型:

dispatch_group_async(
    dispatch_group_t group,
    dispatch_group_t queue,
    ^{
     //處理                   
    });

例子:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);   
dispatch_group_t group = dispatch_group_create();   
dispatch_group_async(group, queue, ^{   
    [NSThread sleepForTimeInterval:1];   
    NSLog(@"Task1");   
});   
dispatch_group_async(group, queue, ^{   
    [NSThread sleepForTimeInterval:2];   
    NSLog(@"Task2");   
});   
dispatch_group_async(group, queue, ^{   
    [NSThread sleepForTimeInterval:3];   
    NSLog(@"Task3");   
});   

//監聽:全部任務完成後執行
dispatch_group_notify(group, dispatch_get_main_queue(), ^{   
    NSLog(@"updateUI");   
});

4. dispatch_barrier_async

前面的任務執行結束後它才執行,並且它後面的任務等它執行完成以後纔會執行
dispatch_barrier_async(
        dispatch_query_t query,
        ^{
            
        });

例子:

dispatch_queue_t queue = dispatch_queue_create("barrier", DISPATCH_QUEUE_CONCURRENT);   
dispatch_async(queue, ^{   
    [NSThread sleepForTimeInterval:2];   
    NSLog(@"dispatch_async1");   
});   
dispatch_async(queue, ^{   
    [NSThread sleepForTimeInterval:4];   
    NSLog(@"dispatch_async2");   
});   
dispatch_barrier_async(queue, ^{   
    NSLog(@"dispatch_barrier_async");   
    [NSThread sleepForTimeInterval:4];   
   
});   
dispatch_async(queue, ^{   
    [NSThread sleepForTimeInterval:1];   
    NSLog(@"dispatch_async3");   
});

5. dispatch_apply

執行某個代碼片斷N次

「等待」其全部的循環運行完畢才往下執行:(同步)

原型:

dispatch_apply(
    size_t iterations,
    dispatch_queue_t queue, 
    ^{
        //執行該段代碼iterations次
    });

6.dispatch_after

延遲一段時間把一項任務提交到dispatch_queue中,返回後不能取消

延遲提交,不是延遲運行

dispatch_after(
    dispatch_time_t time,
    dispatch_queue_t queue,
    ^{
        
    });

dispatch_time (
     dispatch_time_t when,
     int64_t delta :納秒
);
還能夠用dispatch_walltime來建立dispatch_time

when:通常是DISPATCH_TIME_NOW 如今
delta:是具體延遲的時間
    NSEC_PER_SEC :每秒多少納秒
    USEC_PER_SEC :每秒多少微秒
    NSEC_PER_USEC:每微秒多少納秒

延遲10秒:
dispatch_time (
     DISPATCH_TIME_NOW,
     10* NSEC_PER_SEC
);

7.dispatch_once:單例

整個app生命週期內只執行一次

-(instancetype)getSingleton{
    static Singleton *singleton= nil;  
    static dispatch_once_t onceToken;  
    dispatch_once(&onceToken, ^{  
      singleton = [[self alloc] init];
    });  
    return Singleton;   
}

8.dispatch_source_set_timer:(NSTimer)

能夠配合dispatch_source_set_event_handler使用:回調用

原型:

//建立_timer
dispatch_source_create(
    dispatch_source_type_t type, 
    uintptr_t handle, 
    unsigned long mask, 
    dispatch_queue_t queue
);

//設定_timer
dispatch_source_set_timer(
    dispatch_source_t _timer,
    dispatch_time_t start,  //開始時間
    uint64_t interval,  //時間間隔,納秒
    uint64_t leeway //精度:0爲最高精度
);

//_timer執行完了
dispatch_source_set_event_handler:實現Dispatch Source的回調

完整代碼:

//間隔仍是2秒
uint64_t interval = 2 * NSEC_PER_SEC;

//建立一個專門執行timer回調的GCD隊列
dispatch_queue_t queue = dispatch_queue_create("my queue", 0);

//建立Timer
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);

//使用dispatch_source_set_timer函數設置timer參數
dispatch_source_set_timer(_timer, dispatch_time(DISPATCH_TIME_NOW, 0), interval, 0);

//設置回調
dispatch_source_set_event_handler(_timer, ^()
{
    NSLog(@"Timer %@", [NSThread currentThread]);
});

//dispatch_source默認是Suspended狀態,經過dispatch_resume函數開始它
dispatch_resume(_timer);

9. dispatch_suspend, dispatch_resume

提供了「掛起、恢復」隊列的功能

可是不能中止

可是掛起時,當前正在運行的block還會繼續執行完畢,掛起的是後續的block

=====================================================

其餘線程方式:

NSThread http://my.oschina.net/u/2462423/blog/534210

NSOperation http://my.oschina.net/u/2462423/blog/534277

相關文章
相關標籤/搜索