网络多线程-GCD常用函数
2015-11-27 22:09
375 查看
#import "ViewController.h"
#import "XMGPerson.h"
@interface
ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
/*
XMGPerson *p1 = [[XMGPerson alloc]init];
NSLog(@"%@",p1.books);
XMGPerson *p2 = [[XMGPerson alloc]init];
NSLog(@"%@",p2.books);
*/
[self barrier];
}
//一次性代码:整个程序运行过程中只会执行一次
/*不能放在懒加载里面的*/
-(void)once
{
NSLog(@"-----");
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"----once----");
});
}
//延迟执行
-(void)afterDelay
{
NSLog(@"-----start----");
// NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:NO];
// [self performSelector:@selector(run) withObject:nil afterDelay:3.0];
dispatch_queue_t queue =
dispatch_get_main_queue();
// dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
/*
GCD里面的时间是以纳秒为单位的 1秒=10的九次方纳秒
*/
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0
* NSEC_PER_SEC)),queue , ^{
NSLog(@"---block---%@",[NSThread
currentThread]);
});
}
//栅栏函数:控制并发队列里面任务的执行顺序
/*不能使用全局并发队列*/
-(void)barrier
{
//1.创建并发队列
dispatch_queue_t queue =
dispatch_queue_create("xmg",
DISPATCH_QUEUE_CONCURRENT);
//2.异步函数
dispatch_async(queue, ^{
for (NSInteger i =
0; i<10; i++) {
NSLog(@"download1--%zd--%@",i,[NSThread
currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i =
0; i<10; i++) {
NSLog(@"download2--%zd--%@",i,[NSThread
currentThread]);
}
});
//3.使用栅栏函数
dispatch_barrier_async(queue, ^{
NSLog(@"+++++++++++++++++barrier+++++++++++");
});
dispatch_async(queue, ^{
for (NSInteger i =
0; i<10; i++) {
NSLog(@"download3--%zd--%@",i,[NSThread
currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i =
0; i<10; i++) {
NSLog(@"download4--%zd--%@",i,[NSThread
currentThread]);
}
});
dispatch_async(queue, ^{
NSLog(@"download5----%@",[NSThread
currentThread]);
});
}
-(void)run
{
NSLog(@"---run---");
}
@end
#import "XMGPerson.h"
@interface
ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
/*
XMGPerson *p1 = [[XMGPerson alloc]init];
NSLog(@"%@",p1.books);
XMGPerson *p2 = [[XMGPerson alloc]init];
NSLog(@"%@",p2.books);
*/
[self barrier];
}
//一次性代码:整个程序运行过程中只会执行一次
/*不能放在懒加载里面的*/
-(void)once
{
NSLog(@"-----");
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"----once----");
});
}
//延迟执行
-(void)afterDelay
{
NSLog(@"-----start----");
// NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:NO];
// [self performSelector:@selector(run) withObject:nil afterDelay:3.0];
dispatch_queue_t queue =
dispatch_get_main_queue();
// dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
/*
GCD里面的时间是以纳秒为单位的 1秒=10的九次方纳秒
*/
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0
* NSEC_PER_SEC)),queue , ^{
NSLog(@"---block---%@",[NSThread
currentThread]);
});
}
//栅栏函数:控制并发队列里面任务的执行顺序
/*不能使用全局并发队列*/
-(void)barrier
{
//1.创建并发队列
dispatch_queue_t queue =
dispatch_queue_create("xmg",
DISPATCH_QUEUE_CONCURRENT);
//2.异步函数
dispatch_async(queue, ^{
for (NSInteger i =
0; i<10; i++) {
NSLog(@"download1--%zd--%@",i,[NSThread
currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i =
0; i<10; i++) {
NSLog(@"download2--%zd--%@",i,[NSThread
currentThread]);
}
});
//3.使用栅栏函数
dispatch_barrier_async(queue, ^{
NSLog(@"+++++++++++++++++barrier+++++++++++");
});
dispatch_async(queue, ^{
for (NSInteger i =
0; i<10; i++) {
NSLog(@"download3--%zd--%@",i,[NSThread
currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i =
0; i<10; i++) {
NSLog(@"download4--%zd--%@",i,[NSThread
currentThread]);
}
});
dispatch_async(queue, ^{
NSLog(@"download5----%@",[NSThread
currentThread]);
});
}
-(void)run
{
NSLog(@"---run---");
}
@end
相关文章推荐
- 网络多线程-GCD线程间通信
- 网络多线程-GCD的基本使用(2)
- 网络多线程-GCD的基本使用
- 网络多线程-NSThread实现线程间通信
- 网络多线程-线程的安全
- 网络多线程-线程的状态
- 网络多线程-NSThread的使用
- 网络多线程-pthread的使用
- 网络多线程下载图片(SDWebImage)
- 网络多线程-下载图片(不用SDWebImage框架)
- 网络多线程-NSOperation线程间通信
- 网络多线程-NSOperation操作依赖和监听
- 网络多线程-NSOperation的常用的方法
- 网络多线程-NSOperation最大并发数
- 网络多线程-NSOperationQueue基本使用
- 网络多线程-NSOperation的简单使用
- 网络多线程 -单例模式业余练习代码MRC与ARC通用
- 网络多线程-单例模式ARC业余练习代码
- 网络多线程-GCD队列组
- 网络多线程 - 01-掌握-GCD快速迭代