- (void)cast1{
//1.用異步函數(shù),往全局并發(fā)隊(duì)列添加任務(wù)狈醉。異步并發(fā)開(kāi)辟新線程同時(shí)運(yùn)行
//參數(shù):1.優(yōu)先級(jí)<#long identifier#> ?2.為將來(lái)做準(zhǔn)備,通常設(shè)為0<#unsigned long flags#>
//串行:dispatch_queue_create("1234567", NULL);
//獲取全局并發(fā)隊(duì)列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);//并發(fā)
//異步
//把右邊的操作(block)交個(gè)左邊的隊(duì)列(queue)
dispatch_async(queue, ^{
//添加任務(wù)
NSLog(@"下載1---- %@",[NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
//添加任務(wù)
NSLog(@"下載2---- %@",[NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
//添加任務(wù)
NSLog(@"下載3---- %@",[NSThreadcurrentThread]);
});dispatch_async(queue, ^{
//添加任務(wù)
NSLog(@"下載4---- %@",[NSThreadcurrentThread]);
});
NSLog(@"當(dāng)前線程---- %@",[NSThreadcurrentThread]);
//總結(jié):異步函數(shù)具有開(kāi)啟線程的能力----異步并發(fā)執(zhí)行四個(gè)操作(開(kāi)啟了四個(gè)分線程)
}
- (void)cast7 {
//異步并發(fā)
dispatch_queue_t queue? = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSURL* url = [NSURLURLWithString:@"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1467792967&di=399aeff71e6bc005a6439e77e7dad2af&src=http://www.gzmingjin.com.cn/uploadfile/2015/11455153127/23264481380.jpg"];
NSData* data = [NSDatadataWithContentsOfURL:url];
// ???????NSLog(@"---- %@",data);
__blockUIImage* image = [UIImageimageWithData:data];
NSLog(@"----%@",[NSThreadcurrentThread]);
//回到主線程刷新UI
// ??????1. [self performSelectorOnMainThread:@selector(settingImage:) withObject:image waitUntilDone:NO];
// ??????2. [self.image1 performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
// ??????3. [self performSelector:@selector(settingImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO];
// ??????4. [self.image1 performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO];
//回到主線程刷新UI
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"currentThread ---- %@",[NSThreadcurrentThread]);
self.image1.image= image;
});
//優(yōu)點(diǎn):分線程中得到的數(shù)據(jù),可以直接使用,不需要傳值,更加方便和直接
});
}
- (void)settingImage:(UIImage *)image{
self.image1.image= image;
}
- (void)cast8{
//異步并發(fā)
dispatch_async(GLOBAL_QUEUE, ^{
__block UIImage* mage1 = [selfimageWithURL:@"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1467792967&di=399aeff71e6bc005a6439e77e7dad2af&src=http://www.gzmingjin.com.cn/uploadfile/2015/11455153127/23264481380.jpg"];
__block UIImage* mage2 = [selfimageWithURL:@"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1467792967&di=399aeff71e6bc005a6439e77e7dad2af&src=http://www.gzmingjin.com.cn/uploadfile/2015/11455153127/23264481380.jpg"];
//在主線程刷新UI ?dispatch_get_main_queue
dispatch_async(dispatch_get_main_queue(), ^{
self.image1.image= mage1;
self.image2.image= mage2;
//合成圖片
UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100),NO, 0.0);
[mage1drawInRect:CGRectMake(0, 0, 100, 100)];
[mage2drawInRect:CGRectMake(100, 0, 100, 100)];
self.image3.image=UIGraphicsGetImageFromCurrentImageContext();
//關(guān)閉上下文
UIGraphicsEndImageContext();
NSLog(@"currentThread -- %@",[NSThreadcurrentThread]);
});
NSLog(@"111currentThread -- %@",[NSThreadcurrentThread]);
});
}
- (UIImage*)imageWithURL:(NSString*)URLStr{
NSURL* url = [NSURLURLWithString:URLStr];
NSData* data = [NSDatadataWithContentsOfURL:url];
UIImage* image = [UIImageimageWithData:data];
return image;
}
- (void)cast2{
//2.用同步函數(shù)往全局并發(fā)隊(duì)列中添加任務(wù)同步并發(fā)在同一個(gè)線程中同時(shí)運(yùn)行
//獲取全局并發(fā)隊(duì)列
//總結(jié):用同步函數(shù)往全局并發(fā)隊(duì)列中添加任務(wù),由于同步函數(shù)不具有開(kāi)辟線程的能力,所以我們的操作在當(dāng)前線程串行執(zhí)行
}
- (void)cast3{
//3.用同步函數(shù)往串行隊(duì)列中添加任務(wù)同步串行在同一個(gè)線程中一個(gè)一個(gè)運(yùn)行任務(wù)
//獲取全局串行隊(duì)列
//1.隊(duì)列的名字(方便測(cè)試) ?2.隊(duì)列的屬性,一般為NULL
dispatch_queue_t queue =dispatch_queue_create("123456",NULL);
dispatch_sync(queue, ^{
NSLog(@"下載1---- %@",[NSThreadcurrentThread]);
});
//在當(dāng)前線程中串行執(zhí)行
}
- (void)cast4 {
//常用
//4.用異步函數(shù)往串行隊(duì)中添加任務(wù)異步串行開(kāi)辟另一條線程一個(gè)一個(gè)運(yùn)行任務(wù)(只另外開(kāi)辟一條)
// CONCURRENT并發(fā)DISPATCH_QUEUE_SERIAL串行
//總結(jié)
}
- (void)cast9 {
//異步串行
//同時(shí)執(zhí)行下載兩張圖片
//隊(duì)列組
//創(chuàng)建一個(gè)隊(duì)列組
dispatch_group_tgroup =dispatch_group_create();
__blockUIImage* im1 =nil;
//將block(操作)添加到全局并發(fā)隊(duì)列中,然后全局并發(fā)隊(duì)列添加到隊(duì)列組中
dispatch_group_async(group,GLOBAL_QUEUE, ^{
im1 = [selfimageWithURL:@"http://p3.pstatp.com/large/9e00004286008bddc3c"];
NSLog(@"currentThread --- %@",[NSThreadcurrentThread]);
});
__blockUIImage* im2 =nil;
//將block(操作)添加到全局并發(fā)隊(duì)列中,然后全局并發(fā)隊(duì)列添加到隊(duì)列組中
dispatch_group_async(group,GLOBAL_QUEUE, ^{
im2 = [selfimageWithURL:@"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1467792967&di=399aeff71e6bc005a6439e77e7dad2af&src=http://www.gzmingjin.com.cn/uploadfile/2015/11455153127/23264481380.jpg"];
NSLog(@"currentThread --- %@",[NSThreadcurrentThread]);
});
//隊(duì)列組中的所有操作執(zhí)行完調(diào)用該方法---回到主線程刷新UI
dispatch_group_notify(group,dispatch_get_main_queue(), ^{
self.image1.image= im1;
self.image2.image= im2;
//合成圖片
UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100),NO, 0.0);
[im1drawInRect:CGRectMake(0, 0, 100, 100)];
[im2drawInRect:CGRectMake(100, 0, 100, 100)];
self.image3.image=UIGraphicsGetImageFromCurrentImageContext();
//關(guān)閉上下文
UIGraphicsEndImageContext();
NSLog(@"currentThread -- %@",[NSThreadcurrentThread]);
});
}
- (void)cast5 {
//5.用異步函數(shù)往主隊(duì)列中添加任務(wù)主隊(duì)列異步串行
//獲取主隊(duì)列
dispatch_queue_tqueue =dispatch_get_main_queue();
dispatch_async(queue, ^{
NSLog(@"下載1---- %@",[NSThreadcurrentThread]);
});
//總結(jié):放在主隊(duì)列中的任務(wù),都在主線程中串行執(zhí)行分線程管回調(diào)
}
- (void)cast6{
//6.在主線程中用同步函數(shù)往主隊(duì)列中添加任務(wù)主隊(duì)列同步
//卡死無(wú)輸出
//獲取主隊(duì)列
dispatch_queue_t queue =dispatch_get_main_queue();
dispatch_sync(queue, ^{
NSLog(@"下載1---- %@",[NSThreadcurrentThread]);
});
//總結(jié):在主線程中用同步函數(shù)往主隊(duì)列中添加任務(wù)會(huì)造成主線程死鎖下面的代碼不會(huì)執(zhí)行
}
//在分線程中用同步函數(shù)往主隊(duì)列中添加任務(wù)---不會(huì)造成死鎖
[self performSelectorInBackground:@selector(cast6) withObject:nil];
- (void)cast10 {
//延遲函數(shù)
//1.NSObjiect的延遲方法
NSLog(@"111111.currentThread --- %@",[NSThreadcurrentThread]);
[selfperformSelector:@selector(run)withObject:nilafterDelay:2.0];
//2.GCD
//什么時(shí)間在哪個(gè)隊(duì)列中執(zhí)行什么方法
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (2.0 *NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
NSLog(@"1.currentThread --- %@",[NSThreadcurrentThread]);
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (2.0 *NSEC_PER_SEC)),dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"3.currentThread --- %@",[NSThreadcurrentThread]);
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (2.0 *NSEC_PER_SEC)),dispatch_queue_create("123",NULL), ^{
NSLog(@"2.currentThread --- %@",[NSThreadcurrentThread]);
});
}
- (void)run {
NSLog(@"0.currentThread --- %@",[NSThreadcurrentThread]);
}
- (void)cast11{
//dispatch_once函數(shù)中的操作只執(zhí)行一次單列類
for(inti = 0; i < 20;i ++) {
__block int count = 0;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
count ++;
NSLog(@"我執(zhí)行了%d次",count);
});
}
}
宏定義方式
//1.GLOBAL_QUEUE
//2.kPrivate_Global_Queue
//3.kGlobal_Queue
#define GLOBAL_QUEUE ?dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)
#define kAfteTime(x) (int64_t) (x * NSEC_PER_SEC)