GCD_Grand central dispatch

//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;

}

@end


@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

相关文章
相关标签/搜索