OBObjective-c 多线程


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

}

@end


@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

相关文章
相关标签/搜索