dispatch_sync(dispatch_queue_tqueue, dispatch_block_tblock);
dispatch_async(dispatch_queue_tqueue, dispatch_block_tblock);
dispatch_queue_t dispatch_get_global_queue(
dispatch_queue_priority_tpriority,// 隊列的優先級
unsigned longflags);// 此參數暫時無用,用0即可
dispatch_queue_tqueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); //獲得全局併發隊列
dispatch_queue_t
dispatch_queue_create(const char*label, //隊列名稱
dispatch_queue_attr_t attr); //隊列屬性,一般用NULL即可
dispatch_queue_tqueue = dispatch_queue_create("cn.itcast.queue", NULL); // 創建
dispatch_release(queue); // 非ARC需要釋放手動創建的隊列
dispatch_queue_tqueue = dispatch_get_main_queue();
3.各種隊列執行的效果注意
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//執行耗時的異步操作...
dispatch_async(dispatch_get_main_queue(),^{
// 回到主線程,執行UI刷新操作
});
});
[self performSelector:@selector(run)withObject:nil afterDelay:2.0];
// 2秒後再調用self的run方法
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
//2秒後執行這裏的代碼...在哪個線程執行,跟隊列類型有關
});
五.一次性代碼static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//只執行1次的代碼(這裏面默認是線程安全的)
});
dispatch_group_tgroup = dispatch_group_create();
dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//執行1個耗時的異步操作
});
dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//執行1個耗時的異步操作
});
dispatch_group_notify(group,dispatch_get_main_queue(),^{
//等前面的異步操作都執行完畢後,回到主線程...
});
七.單例模式
#if __has_feature(objc_arc)
// ARC
#else
// MRC
#endif
static id_instance;
+ (id)allocWithZone:(struct _NSZone*)zone {
if (_instance == nil) { // 防止頻繁加鎖
@synchronized(self) {
if (_instance == nil) { // 防止創建多次
_instance = [super allocWithZone:zone];
}
}
}
return _instance;
}
+ (instancetype)sharedMusicTool {
if (_instance == nil) { // 防止頻繁加鎖
@synchronized(self) {
if (_instance == nil) { // 防止創建多次
_instance = [[self alloc] init];
}
}
}
return _instance;
}
- (id)copyWithZone:(struct _NSZone*)zone {
return _instance;
}
- (id)retain { return self; }
- (NSUInteger)retainCount { return 1; }
- (oneway void)release{}
- (id)autorelease { return self; }
<span style="font-size:18px;">//1.串行隊列 + 同步執行函數
//結論1. 沒有開啓新線程
//結論2. 按順序執行
//開發中布經常用
- (void)serialAddSync
{
NSLog(@"serialAddSycn%@",[NSThread currentThread]);
//1.創建串行隊列
dispatch_queue_t queue = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL);
//2.同步執行函數 將任務添加到串行隊列中 交給同步執行函數執行
dispatch_sync(queue, ^{
//任務1
NSLog(@"耗時任務1:%@",[NSThread currentThread]);
});
//往串行隊列中 添加多個任務
dispatch_sync(queue, ^{
//任務2
NSLog(@"耗時任務2:%@",[NSThread currentThread]);
});dispatch_sync(queue, ^{
//任務3
NSLog(@"耗時任務3:%@",[NSThread currentThread]);
});
}
</span>
//2.串行隊列 + 異步執行函數
//結論1.開啓新線程
//2.同一隊列的任務由同一個線程來執行,肯定是按順序執行
//一個串行隊列對應一條線程
- (void)serialAddAsync{
NSLog(@"serialAddAsycnBegan%@",[NSThread currentThread]);
//1.創建串行隊列
dispatch_queue_t queue = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL);
dispatch_queue_t queue1 = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL);
//2.異步執行函數 將任務添加到串行隊列中 交給異步執行函數執行
dispatch_async(queue, ^{
//任務1
NSLog(@"耗時任務1:%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
//任務2
NSLog(@"耗時任務2:%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
//任務3
NSLog(@"耗時任務3:%@",[NSThread currentThread]);
});
dispatch_async(queue1, ^{
//任務1
NSLog(@"耗時任務4:%@",[NSThread currentThread]);
});
dispatch_async(queue1, ^{
//任務2
NSLog(@"耗時任務5:%@",[NSThread currentThread]);
});
NSLog(@"serialAddAsycnEnd%@",[NSThread currentThread]);
}
//3.併發隊列 + 同步執行函數
//沒有開啓新線程
//任務順序執行(在同一線程(當前線程)中執行)
- (void)concurrentAddSync
{
NSLog(@"concurrentAddSync%@",[NSThread currentThread]);
//1.創建全局併發隊列
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
//2.將任務添加到隊列中
dispatch_sync(queue, ^{
//任務
NSLog(@"任務1:%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
//任務
NSLog(@"任務2:%@",[NSThread currentThread]);
});dispatch_sync(queue, ^{
//任務
NSLog(@"任務3:%@",[NSThread currentThread]);
});
}
//4.併發隊列 + 異步執行函數(開發中使用最多)
//開啓多條線程
//任務同時執行的
- (void)concurrentAddAsync {
NSLog(@"concurrentAddAsync%@",[NSThread currentThread]);
//1.創建全局併發隊列
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
//2.將任務添加到隊列
dispatch_async(queue, ^{
//任務
NSLog(@"任務1:%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
//任務
NSLog(@"任務2:%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
//任務
NSLog(@"任務3:%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
//任務
NSLog(@"任務4:%@",[NSThread currentThread]);
});
}
//5.主隊列 + 同步執行函數
//1.如果當前線程爲主線程 會照成主隊列和主線程中的任務相互等待 無法執行 主線程卡死
//主隊列的任務都交給主線程執行
//主隊列和主線程中的任務相互等待 無法執行
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"renwu1%@",[NSThread currentThread]);
});
NSLog(@"touchesEnd%@",[NSThread currentThread]);
//6.主隊列 + 異步執行函數 (開發中需要回到主線程 使用 主隊列 + 異步函數)
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"renwu1%@",[NSThread currentThread]);
});
NSLog(@"touchesEnd%@",[NSThread currentThread]);