多线程的简单使用

  • 时间:2020-04-24 20:56 作者:路灯下的黑猫 来源: 阅读:515
  • 扫一扫,手机访问
摘要:////? ViewController.m//? Test////? Created by 路灯下的黑猫 on 2018/8/24.//? Copyright ? 2018年 张浩. All rights reserved.//#import "ViewController.h"staticNSI

//

//? ViewController.m

//? Test

//

//? Created by 路灯下的黑猫 on 2018/8/24.

//? Copyright ? 2018年 张浩. All rights reserved.

//

#import "ViewController.h"

staticNSIntegerconstAddTag =100;

@interface ViewController ()

@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong) NSArray *urlArr;

@end

@implementation ViewController

- (void)viewDidLoad {

? ? [super viewDidLoad];


? ? self.urlArr = @[@"https://pic.songma.com/wenzhang/20200424/t2i5ppyna4h426.jpg",

? ? ? ? ? ? ? ? ? ? @"https://pic.songma.com/wenzhang/20200424/jtbrdzctrsg427.png",

? ? ? ? ? ? ? ? ? ? @"https://pic.songma.com/wenzhang/20200424/xy1bse2acwu428.png",

? ? ? ? ? ? ? ? ? ? @"https://pic.songma.com/wenzhang/20200424/m5upujj4053429.jpg",

? ? ? ? ? ? ? ? ? ? @"https://pic.songma.com/wenzhang/20200424/t2i5ppyna4h426.jpg",

? ? ? ? ? ? ? ? ? ? @"https://pic.songma.com/wenzhang/20200424/jtbrdzctrsg427.png",

? ? ? ? ? ? ? ? ? ? @"https://pic.songma.com/wenzhang/20200424/xy1bse2acwu428.png",

? ? ? ? ? ? ? ? ? ? @"https://pic.songma.com/wenzhang/20200424/m5upujj4053429.jpg",

? ? ? ? ? ? ? ? ? ? @"https://pic.songma.com/wenzhang/20200424/t2i5ppyna4h426.jpg",

? ? ? ? ? ? ? ? ? ? @"https://pic.songma.com/wenzhang/20200424/jtbrdzctrsg427.png",

? ? ? ? ? ? ? ? ? ? @"https://pic.songma.com/wenzhang/20200424/xy1bse2acwu428.png",

? ? ? ? ? ? ? ? ? ? @"https://pic.songma.com/wenzhang/20200424/m5upujj4053429.jpg"];



? ? /*简单线程**/

? ? //[self thread];

? ? /* 队列操作**/

? ? //[self operation];


? ? /*GCD**/

? ? [selfGCD];


}

/********** 线程Thread 操作 ************/

- (void)thread {



? ? _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];

? ? [self.view addSubview:_imageView];

? ? UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];

? ? button.frame=CGRectMake(50,500,220,25);

? ? [buttonsetTitle:@"加载图片" forState:UIControlStateNormal];

? ? //增加方法

? ? [buttonaddTarget:self action:@selector(threadClick) forControlEvents:UIControlEventTouchUpInside];

? ? [self.viewaddSubview:button];



}

- (void)threadClick {


? ? [NSThread detachNewThreadSelector:@selector(threadRun) toTarget:self withObject:nil];


}

- (void)threadRun {

? ? NSLog(@"callMainbefore_1");

? ? NSURL*url=[NSURLURLWithString:self.urlArr.firstObject];

? ? NSData *data=[NSData dataWithContentsOfURL:url];

? ? NSLog(@"callMainbefore_2");


? ? /*


?? ? 直到 updateUI: 做完了 能否才开始往下操作


?? ? waitUntilDone:NO

?? ? 2016-06-02 17:13:47.618 多线程解决[10042:208753] callMainbefore_1

?? ? 2016-06-02 17:13:47.673 多线程解决[10042:208753] callMainbefore_2

?? ? 2016-06-02 17:13:47.674 多线程解决[10042:208753] callMainAfter

?? ? 2016-06-02 17:13:52.675 多线程解决[10042:208668] callMain


?? ? waitUntilDone:YES

?? ? 2016-06-02 17:16:05.525 多线程解决[10062:210446] callMainbefore_1

?? ? 2016-06-02 17:16:05.607 多线程解决[10062:210446] callMainbefore_2

?? ? 2016-06-02 17:16:10.609 多线程解决[10062:210347] callMain

?? ? 2016-06-02 17:16:10.613 多线程解决[10062:210446] callMainAfter


?? ? **/


? ? [self performSelectorOnMainThread:@selector(updateUI:) withObject:data waitUntilDone:YES];

? ? NSLog(@"callMainAfter");




}

- (void)updateUI:(NSData*)imageData {


? ? sleep(5);

? ? NSLog(@"callMain");

? ? self.imageView.image= [UIImageimageWithData:imageData];


}

/********** 线程Thread 结束 ************/

/********** 队列 操作 ************/

- (void)operation {



? ? CGFloatspace =10;

? ? CGFloatimageWH = (320-10*5)/4.0;


? ? for(NSIntegeri =0; i

? ? ? ? UIImageView* imageView = [[UIImageViewalloc]init];

? ? ? ? [self.viewaddSubview:imageView];

? ? ? ? imageView.frame=CGRectMake(space +(space+imageWH)*(i%4), space +(space+imageWH)*(i/4), imageWH, imageWH);

? ? ? ? imageView.tag=AddTag+ i;

? ? }


? ? UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];

? ? button.frame=CGRectMake(50,500,220,25);

? ? [buttonsetTitle:@"加载图片" forState:UIControlStateNormal];

? ? [buttonaddTarget:self action:@selector(operationClick) forControlEvents:UIControlEventTouchUpInside];

? ? [self.viewaddSubview:button];



}

- (void)operationClick {



? ? NSOperationQueue * queue = [[NSOperationQueue alloc]init];

? ? queue.maxConcurrentOperationCount = 2;

? ? __weak typeof(self)weakSelf = self;

? ? for(NSIntegeri =0; i

? ? ? ? UIImageView* imageView = [self.viewviewWithTag:AddTag+i];

? ? ? ? imageView.image=nil;

? ? ? ? NSBlockOperation * blockOp = [NSBlockOperation blockOperationWithBlock:^{


? ? ? ? ? ? NSURL*url=[NSURLURLWithString:weakSelf.urlArr[i]];

? ? ? ? ? ? NSData*data=[NSDatadataWithContentsOfURL:url];


? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{

? ? ? ? ? ? ? ? //升级UI 需要主线程

? ? ? ? ? ? ? ? imageView.image= [UIImageimageWithData:data];


? ? ? ? ? ? });


? ? ? ? }];

? ? ? ? if(i >0) {

? ? ? ? ? ? /* 就是 依赖前面的**/

? ? ? ? ? ? NSBlockOperation* preOperation = queue.operations[i-1];

? ? ? ? ? ? [blockOpaddDependency:preOperation];

? ? ? ? }

? ? ? ? [queueaddOperation:blockOp];


? ? }


}

/********** 队列 操作结束 ************/

/****************** GCD ******************/

- (void)GCD {


? ? CGFloatspace =10;

? ? CGFloatimageWH = (320-10*5)/4.0;


? ? for(NSIntegeri =0; i

? ? ? ? UIImageView* imageView = [[UIImageViewalloc]init];

? ? ? ? [self.viewaddSubview:imageView];

? ? ? ? imageView.frame=CGRectMake(space +(space+imageWH)*(i%4), space +(space+imageWH)*(i/4), imageWH, imageWH);

? ? ? ? imageView.tag=AddTag+ i;

? ? }


? ? UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];

? ? button.frame=CGRectMake(50,500,220,25);

? ? [buttonsetTitle:@"加载图片" forState:UIControlStateNormal];

? ? //增加方法

? ? [buttonaddTarget:self action:@selector(GCDClick) forControlEvents:UIControlEventTouchUpInside];

? ? [self.viewaddSubview:button];

}

/* 串行执行**/

- (void)GCDClick {


? ? __weak typeof(self)weakSelf = self;

? ? dispatch_queue_tserialQueue =dispatch_queue_create("LeeGCDQueue",DISPATCH_QUEUE_SERIAL);

? ? for(NSIntegeri =0; i

? ? ? ? UIImageView* imageView = [self.viewviewWithTag:AddTag+i];

? ? ? ? imageView.image=nil;

? ? ? ? dispatch_async(serialQueue, ^{


? ? ? ? ? ? NSURL*url=[NSURLURLWithString:weakSelf.urlArr[i]];

? ? ? ? ? ? NSData*data=[NSDatadataWithContentsOfURL:url];

? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{


? ? ? ? ? ? ? ? //升级UI 需要主线程

? ? ? ? ? ? ? ? imageView.image= [UIImageimageWithData:data];

? ? ? ? ? ? });



? ? ? ? });


? ? }


}

///* 并发执行**/

//- (void)GCDClick {

//

//? ? __weak typeof(self)weakSelf = self;

//? ? dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

//? ? for (NSInteger i = 0 ; i < self.urlArr.count; i++) {

//? ? ? ? UIImageView * imageView = [self.view viewWithTag:AddTag+i];

//? ? ? ? imageView.image = nil;

//? ? ? ? dispatch_async(globalQueue, ^{

//

//? ? ? ? ? ? NSURL *url=[NSURL URLWithString:weakSelf.urlArr[i]];

//? ? ? ? ? ? NSData *data=[NSData dataWithContentsOfURL:url];

//? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{

//

//? ? ? ? ? ? ? ? //升级UI 需要主线程

//? ? ? ? ? ? ? ? imageView.image = [UIImage imageWithData:data];

//? ? ? ? ? ? });

//

//

//? ? ? ? });

//

//? ? }

//

//}

/****************** GCD 结束 ******************/

@end

  • 全部评论(0)
最新发布的资讯信息
【系统环境|】2FA验证器 验证码如何登录(2024-04-01 20:18)
【系统环境|】怎么做才能建设好外贸网站?(2023-12-20 10:05)
【系统环境|数据库】 潮玩宇宙游戏道具收集方法(2023-12-12 16:13)
【系统环境|】遥遥领先!青否数字人直播系统5.0发布,支持真人接管实时驱动!(2023-10-12 17:31)
【系统环境|服务器应用】克隆自己的数字人形象需要几步?(2023-09-20 17:13)
【系统环境|】Tiktok登录教程(2023-02-13 14:17)
【系统环境|】ZORRO佐罗软件安装教程及一键新机使用方法详细简介(2023-02-10 21:56)
【系统环境|】阿里云 centos 云盘扩容命令(2023-01-10 16:35)
【系统环境|】补单系统搭建补单源码搭建(2022-05-18 11:35)
【系统环境|服务器应用】高端显卡再度登上热搜,竟然是因为“断崖式”的降价(2022-04-12 19:47)
手机二维码手机访问领取大礼包
返回顶部