//c++
// ViewController.m異步
// GCD_Grand central dispatchasync
//ide
// Created by dc008 on 15/12/25.spa
// Copyright © 2015年 崔曉宇. All rights reserved..net
//線程
#import "ViewController.h"3d
@interface ViewController ()orm
{隊列
NSMutableArray *_MutableArray;
float testCo;
NSArray *_array;
}
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_MutableArray = [NSMutableArray array];
_array = @[@"http://pic12.nipic.com/20110121/3556745_181933155140_2.jpg",
@"http://pic31.nipic.com/20130722/12196046_160624534194_2.jpg",
@"http://pic4.nipic.com/20091118/3047572_163336087407_2.jpg",
@"http://pic.nipic.com/2008-05-29/200852973848511_2.jpg",
@"http://pic41.nipic.com/20140504/18401726_095421197182_2.jpg",
@"http://pic51.nipic.com/file/20141024/13035204_155709166956_2.jpg",
@"http://pic36.nipic.com/20131125/8821914_113934565000_2.jpg",
@"http://pic31.nipic.com/20130722/12196046_160637651196_2.jpg",
@"http://pic28.nipic.com/20130412/3718408_231850588113_2.jpg",
@"http://pica.nipic.com/2007-06-06/20076615744388_2.jpg",
@"http://pic23.nipic.com/20120901/8737320_222046006000_2.jpg",
@"http://pic12.nipic.com/20110119/3556745_180736335138_2.jpg",
@"http://pic27.nipic.com/20130226/6806713_165327321179_2.jpg",
@"http://pic18.nipic.com/20111202/8920105_164704212146_2.jpg",
@"http://img.taopic.com/uploads/allimg/110914/8879-11091423505866.jpg"
];
[self layout];
}
- (void)layout{
for (int r =0; r<5; r++) {
for (int c = 0; c<3; c++) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(18.75+c*118.75, 80+r*118.75, 100, 100)];
imageView.backgroundColor = [UIColor grayColor];
[self.view addSubview:imageView];
[_MutableArray addObject:imageView];
}
}
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 20, 375, 40)];
[button setTitle:@"開始加載圖片" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(GCDdemoRun) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
//加載圖片
- (void)loadImage : (int) index{
//打印當前操做線程
NSLog(@"%@",[NSThread currentThread]);
//獲取圖片數據
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://pic4.nipic.com/20091118/3047572_163336087407_2.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)GCDdemoRun3{
//隊列組
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)GCDdemoRun2{
//全局隊列(可開啓多個線程)
//參數1:線程優先級(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);//休眠1秒
});
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end