ios开发之多线程--GCD介绍
来源:互联网 发布:docker安装windows 10 编辑:程序博客网 时间:2024/06/05 15:11
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:nilafterDelay: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 = [superallocWithZone:zone];
}
}
}
return _instance;
}
+ (instancetype)sharedMusicTool {
if (_instance ==nil) { // 防止频繁加锁
@synchronized(self) {
if (_instance ==nil) { // 防止创建多次
_instance = [[selfalloc] init];
}
}
}
return _instance;
}
- (id)copyWithZone:(struct_NSZone*)zone {
return _instance;
}
- (id)retain {return self; }
- (NSUInteger)retainCount {return 1; }
- (onewayvoid)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, ^{//任务1NSLog(@"耗时任务1:%@",[NSThread currentThread]);});dispatch_async(queue, ^{//任务2NSLog(@"耗时任务2:%@",[NSThread currentThread]);});dispatch_async(queue, ^{//任务3NSLog(@"耗时任务3:%@",[NSThread currentThread]);});dispatch_async(queue1, ^{//任务1NSLog(@"耗时任务4:%@",[NSThread currentThread]);});dispatch_async(queue1, ^{//任务2NSLog(@"耗时任务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]);
- iOS开发之多线程(GCD介绍)
- ios开发之多线程--GCD介绍
- iOS开发之多线程—GCD介绍
- iOS 开发之多线程GCD介绍
- iOS开发之多线程GCD
- iOS开发之多线程GCD
- ios开发之多线程GCD
- iOS 开发之多线程之GCD
- IOS开发之多线程与GCD
- IOS开发之多线程与GCD
- iOS开发之多线程编程:GCD
- iOS开发实践之多线程(GCD)
- iOS之多线程开发(NSThread,NSOperation,GCD)
- iOS开发之多线程NSThread,NSOperation,GCD
- IOS之多线程GCD
- ios 之多线程 GCD
- ios开发进阶之多线程01 线程 GCD
- iOS开发之多线程--多线程简单介绍
- Python
- 继承中的构造方法
- 关于结构体的操作符重载和定义在内部的函数
- MyEclipse10.x添加jadClipse反编译插件
- 2015暑期多校训练第一场 1,2,3
- ios开发之多线程--GCD介绍
- Linux 硬限制和软限制 http://www.52ml.net/2624.html
- python list元素为dict时的排序
- Repeated DNA Sequences —— Leetcode(教训,重做)
- hdu 1067 Gap+BFS+hash
- LA 4670 Dominating Patterns (ac自动机)
- C语言hash函数
- USACO 1.3 Mixing Milk
- 树中最长路径 树形DP HDU 3534 Tree