//GCD_Grand central dispatchc++
//
// ViewController.m
// GCD_Grand central dispatch
//
// Created by DC020 on 15/12/25.
// Copyright (c) 2015年 Bill. All rights reserved.
//
#import "ViewController.h"
#define ROW 3
#define CONLUMN 5
#define imageCount WINTH * CONLUNM
@interface ViewController (){
NSMutableArray *_MutableArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_MutableArray = [NSMutableArray array];
for (int c = 0; c < CONLUMN; c++) {
for (int r = 0; r < ROW; r++) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(18.75+r*118.75, 70+c*118.75, 100, 100)];
imageView.backgroundColor = [UIColor redColor];
[self.view addSubview:imageView];
[_MutableArray addObject:imageView];
}
}
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(50, 40, 275, 20)];
[button addTarget:self action:@selector(GCDdemoRun) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"加载图片" forState:UIControlStateNormal];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.view addSubview:button];
}
-(void)loadImage:(int)Index{
//打印当前线程
NSLog(@"%@",[NSThread currentThread]);
//获取图片数据
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:@"http://b.zol-img.com.cn/desk/bizhi/image/7/2560x1600/1447404575613.jpg"]]];
//获取主队列,更新UI
dispatch_queue_t mainQueue = dispatch_get_main_queue();
//同步执行
//当前线程正在执行任务时,不会开启新线程
dispatch_sync(mainQueue, ^{
UIImage *image = [UIImage imageWithData:data];
UIImageView *imageView = _MutableArray[Index];
imageView.image = image;
});
}
-(void)GCDdemoRun{
//队列组
dispatch_group_t groupQueue = dispatch_group_create();
//组1开始异步执行
dispatch_group_async(groupQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"组1任务完成");
});
//组2开始异步执行
dispatch_group_async(groupQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"组2任务完成");
});
//监听组队列任务完成
dispatch_group_notify(groupQueue, dispatch_get_main_queue(), ^{
NSLog(@"组队列任务结束");
});
}
//锁🔒机制
//IOS中经常使用两种方法
//1.NSLock
//2.@synchronized
//-(void)GCDdemoRun{
// //全局队列(可开启多个线程)
// //参数:优先级(high,default,low)
// //参数2:标记参数,目前没有用,通常写0
// dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// //建立线程执行任务
// for (int i = 0; i < 15; i++) {
// //异步执行队列任务
// dispatch_async(globalQueue, ^{
// [self loadImage:i];
// });
// }
//}
//-(void)GCDdemoRun{
// //串行队列
// //参数:队列名称,队列类型
// dispatch_queue_t serialQueue = dispatch_queue_create("mySerialQueue", DISPATCH_QUEUE_SERIAL);
// //建立1个线程(执行15个任务)
// for(int i = 0; i < 15;i++){
// //异步执行队列任务
// //因为是串行队列,因此不会开启新的线程
// dispatch_async(serialQueue, ^{
// [self loadImage:i];
//// sleep(1);//休眠一秒
// });
// }
//}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//shareSDK异步
关键要用button添加事件;async
// 解决资源抢夺问题(买车票)ide
//
// ViewController.m
// 解决资源抢夺问题(买车票)
//
// Created by DC020 on 15/12/25.
// Copyright (c) 2015年 Bill. All rights reserved.
//
#import "ViewController.h"
@interface ViewController (){
NSLock *_lock;
}
//nonatomic属性读取的是内存数据(寄存器计算好的结果)
//atomic属性保持直接读取寄存器的数据,****[这样就不会出现一个线程正在修改数据,而另外一个线程读取了修改以前的数据]****永远保证同时只有一个线程在访问一个属性
@property(atomic,strong)NSMutableArray *tickets;//票
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_lock =[[NSLock alloc]init];
_tickets = [NSMutableArray array];
[_tickets addObjectsFromArray:@[@"1.上铺",@"2.中铺",@"3.下铺"]];
[self beginSell];
}
-(void)buyTicket:(int)buyer{
NSLog(@"%d",buyer);//10位顾客准备购买
//使用时,把须要加锁的代码放在lock和unlock之间。
//当一个线程A进入加锁代码后,另外一个线程B他就没法访问,只有当线程A执行完加锁的任务之后,B线程才能访问加锁代码
// [_lock lock];//上锁
@synchronized(self){
if(_tickets.count > 0){
NSLog(@"%d号顾客买到:%@",buyer,[_tickets lastObject]);
[_tickets removeLastObject];
}
else{
NSLog(@"%d号顾客晚了一步,票已卖完了!!!",buyer);
}
}
// [_lock unlock];//解锁
}
-(void)beginSell{
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//建立10个线程用来抢票(10我的来买票)
for(int i = 0; i < 10 ; i++){
//异步执行
dispatch_async(globalQueue, ^{
[self buyTicket:i];
});
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
atom