// ViewController.m文件c++
// GCD 多線程多線程
//異步
// Created by DC017 on 15/12/25.async
// Copyright © 2015年 DC017. All rights reserved.spa
//.net
#import "ViewController.h"線程
@interface ViewController ()3d
{orm
UIImageView * imageView;隊列
NSMutableArray * muarray;
NSArray * arrayWANGZHI;
}
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
arrayWANGZHI=@[
@"http://pic19.nipic.com/20120310/8061225_093309101000_2.jpg",
@"http://pic19.nipic.com/20120310/8061225_093309101000_2.jpg",
@"http://pic19.nipic.com/20120310/8061225_093309101000_2.jpg",
@"http://img.tuku.cn/file_big/201409/048f4c825de5442e952ddc5e28563100.jpg",
@"http://p7.qhimg.com/t01308022902bb3cc15.jpg",
@"http://www.33lc.com/article/UploadPic/2012-8/2012815167420064.jpg",
@"http://d.3987.com/jmfghjgqbz.120925/004.jpg",
@"http://img.bbs.duba.net/month_1011/1011020907c8b129640ca5a7e7.jpg",
@"http://www.deskcar.com/desktop/car/201337162241/6.jpg",
@"http://www.deskcar.com/desktop/car/201337162241/6.jpg",
@"http://pic11.nipic.com/20101117/778850_171649021175_2.jpg",
@"http://pic.4j4j.cn/upload/pic/20130305/6399c64adb.jpg",
@"http://kuoo8.com/wall_up/hsf2288/200911/200911031144096643.jpg",
@"http://pic.4j4j.cn/upload/pic/20130305/6399c64adb.jpg",
@"http://www.deskcar.com/desktop/car/2005/2007121201442/10.jpg"
];
[self layout];
[self GCDdemoRun];
}
-(void)layout{
muarray=[[NSMutableArray alloc]initWithCapacity:10];
for (int r=0; r<5; r++) {
for (int c=0; c<3; c++) {
imageView=[[UIImageView alloc]initWithFrame:CGRectMake(18.75+c*118.75, 20+r*118.75, 100, 100)];
imageView.backgroundColor=[UIColor orangeColor];
[self.view addSubview:imageView];
[muarray addObject:imageView];
}
}
}
-(void)loadImage :(int)index{
NSLog(@"%@",[NSThread currentThread]);
//獲取圖片數據
NSData * data=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:arrayWANGZHI[index]]];
//獲取主隊列 更新UI
dispatch_queue_t mainQueue=dispatch_get_main_queue();
//同步執行
//當前線程正在執行任務時,不會開啓新線程
dispatch_sync(mainQueue, ^{
UIImage * image=[UIImage imageWithData:data];
UIImageView * imageview=muarray[index];
imageview.image=image;
});
}
#pragma mark 串行隊列
//-(void)GCDdemoRun{
// //實現串行隊列
// //隊列名稱和對列類型
// dispatch_queue_t serialQueue=dispatch_queue_create("mySerialQueue", DISPATCH_QUEUE_SERIAL);
// //建立一個線程(執行15個任務)
// for (int i=0; i<15; i++) {
// //建立異步執行隊列任務
// //因爲建立串行隊列因此不會開啓新的線程
// dispatch_async(serialQueue, ^{
// [self loadImage:i];
// //休眠
// sleep(1);
//
// });
// }
//}
#pragma mark 全局隊列
//-(void)GCDdemoRun{
// //全局隊列
// //參數1 線程優先級
// //參數2 標記參數,目前沒有用 通常寫0
// dispatch_queue_t globalQueue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// //建立線程執行任務
// for (int a=0; a<15; a++) {
// dispatch_async(globalQueue, ^{
// [self loadImage:a];
// });
// }
//}
//- (void)didReceiveMemoryWarning {
// [super didReceiveMemoryWarning];
// // Dispose of any resources that can be recreated.
//}
#pragma mark 隊列組
-(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 完成");
for (int a=0; a<5; a++) {
[self loadImage:a];
}
});
//組2 開始異步執行任務
dispatch_group_async(groupQueue,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
for (int s=5;s<15 ; s++) {
[self loadImage:s];
}
});
//監聽組隊列完成
dispatch_group_notify(groupQueue, dispatch_get_main_queue(), ^{
NSLog(@"組隊列任務所有完成");
});
}
#pragma mark 鎖機制
//IOS 中經常使用兩種方法
//1.NSLock
//2.@synchronized
//
@end