这里主要使用到了dispatch的一些方法
直接拖控件,使用button和image。
重点
1.线程的类型是dispatch_queue_t
2.获得主队列的方法是dispatch_get_main_queue();
3.向队列中添加方法dispatch_async(queue,^{ });
4.dispatch_queue_create(“队列的名字”,同步/异步(DISPATCH_QUEUE_SERIAL))来创建队列。
5.输出当前队列[NSThread currentThread]
6.自己创建的队列要进行释放
7.获取系统队列dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEAULT,0)
这次老师讲的不详细,要自己补充知识了
#import "ViewController.h"
@interface ViewController ()
- (IBAction)serial:(UIButton *)sender;
- (IBAction)showImage:(UIButton *)sender;
@property (retain, nonatomic) IBOutlet UIImageView *imgView;
- (IBAction)Parallels:(UIButton *)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
#pragma mark ------------GCD中的几个概念
//1.Queue队列 dispath queue 存放的是一个或多个对象(操作对象)
//2.Task 任务 具有一定功能的代码段(函数,block)
//3.NSThread 为了保证task的任务的顺利执行,系统会开辟适当的thread(线程),在开辟的线程中,执行Task
//4.GCD Grand Central Dispatch
//5.Queue分为两种类型
//①.串行队列(SerialQueue)
// 特点:执行的任务是先进先出(FIFO),排在队列最前面的任务先执行,排在后面的后执行
//②并行队列 (conCurrentQueue)
// 特点:先进先出(FIFO),一旦第一个任务开始执行,后面的任务也开始执行(无需等待前一个任务是否执行完成)
//获取主队列 -------
dispatch_get_main_queue();
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark ------------串行队列
- (IBAction)serial:(UIButton *)sender {
//如果你想让你的任务在串行队列中执行,要使用SerialQueue
//如何为获取串行队列(SerialQueue),有两种途径:
//第一种:使用系统自带的mainQueue(主队列)
//注意:这个mainQueue里只有一个线程,而且是主线程
dispatch_queue_t queue = dispatch_get_main_queue();
//采用异步方式向队列中添加任务
dispatch_async(queue, ^{
NSLog(@"任务一%@,%d",[NSThread currentThread],[NSThread isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务二%@,%d",[NSThread currentThread],[NSThread isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务三%@,%d",[NSThread currentThread],[NSThread isMainThread]);
});
//第二种:使用自己创建的队列
//第一个参数表示队列的名字(域名倒写)
//第二个参数:决定当前这个方法创建出来的Queue是否是串行队列
dispatch_queue_t queue1 = dispatch_queue_create("com.lanou4g.myQueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue1, ^{
NSLog(@"任务五%@,%d",[NSThread currentThread],[NSThread isMainThread]);
});
dispatch_async(queue1, ^{
NSLog(@"任务四%@,%d",[NSThread currentThread],[NSThread isMainThread]);
});
//在MRC下,任何一个带create创建的队列,一定要记得释放
dispatch_release(queue1);
}
- (IBAction)showImage:(UIButton *)sender {
__block ViewController *VC = self;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
//创建网址对象
NSString *string = @"http://image.zcool.com.cn/56/13/1308200901454.jpg";
NSURL * url = [NSURL URLWithString:string];
NSData * data = [NSData dataWithContentsOfURL:url];
UIImage * iamge = [UIImage imageWithData:data];
//获取主线程,使用GCD的话,你要先获得主队列,因为只有主线程中
//获取主队列
dispatch_queue_t queue1 = dispatch_get_main_queue();
dispatch_async(queue1, ^{
//当前是主线程的任务
VC.imgView.image = iamge;
NSLog(@"%@",[NSThread currentThread]);
});
});
}
#pragma mark --------------并行队列
- (IBAction)Parallels:(UIButton *)sender {
//如果你想让你的任务并发执行,我们使用conCurrenQueue
//获取并行队列,有两种方式
//①使用系统提供的
// 第一种(系统提供的):全局队列(globalQueue)
//第一个参数表示队列的优先级(HIGH > DEFALUT > LOW >BACKGROND
//第二个参数是系统预留,现在没有。
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 2);
//采用异步方式向队列中添加代码
// dispatch_async(queue, ^{
// NSLog(@"任务一%@,%d",[NSThread currentThread],[NSThread isMainThread]);
// });
// dispatch_async(queue, ^{
// NSLog(@"任务二%@,%d",[NSThread currentThread],[NSThread isMainThread]);
// });
// dispatch_async(queue, ^{
// NSLog(@"任务三%@,%d",[NSThread currentThread],[NSThread isMainThread]);
// });
//自己创建并行队列
dispatch_queue_t queue2 = dispatch_queue_create("com.lanou4g.myGlobleQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue2, ^{
NSLog(@"任务一%@,%d",[NSThread currentThread],[NSThread isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务二%@,%d",[NSThread currentThread],[NSThread isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务三%@,%d",[NSThread currentThread],[NSThread isMainThread]);
});
}
- (void)dealloc {
[_imgView release];
[super dealloc];
}
@end