-
什么叫做线程间通信
- 在1个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信
线程间通信的体现
1个线程传递数据给另1个线程
在1个线程中执行完特定任务后,转到另1个线程继续执行任务
线程间通信常用方法
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait;
- 示例 - 图片下载
代码(PS:里面用到的图片URL取至百度,如果有版权问题,请告知本人,本人会尽快删除):
//
// ViewController.m
// CommunicationBetweenThreads-线程间通信
//
// Created by wenjim on 17/4/6.
// Copyright © 2017年 WenJim. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UIImageView * imageView;
@property (nonatomic,strong) UIButton * clickBtn;
@end
@implementation ViewController
-(UIImageView *)imageView
{
if (!_imageView) {
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width / 2 - 40, self.view.bounds.size.height / 2 - 150, 100, 100)];
_imageView.backgroundColor = [UIColor redColor];
_imageView.clipsToBounds = YES;
_imageView.contentMode = UIViewContentModeScaleAspectFill;
}
return _imageView;
}
-(UIButton *)clickBtn
{
if (!_clickBtn) {
_clickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_clickBtn.frame = CGRectMake(self.view.bounds.size.width / 2 - 40, self.view.bounds.size.height / 2 - 10, 80, 20);
[_clickBtn setTitle:@"下载" forState:UIControlStateNormal];
[_clickBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[_clickBtn addTarget:self action:@selector(setDownloadBtn:) forControlEvents:UIControlEventTouchUpInside];
}
return _clickBtn;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpAllControls];
}
#pragma mark - UI控件布局
-(void)setUpAllControls
{
[self.view addSubview:self.imageView];
[self.view addSubview:self.clickBtn];
}
/*
* 获取当前的时间
* // 获得当前的时间
* NSDate * startDate = [NSDate date];
*
* // 获取结束时间
* NSDate * endDate = [NSDate date];
*
*
*/
/*
* 用C语言获取当前时间
* // 获得当前时间 - 绝对时间
* CFTimeInterval start = CFAbsoluteTimeGetCurrent();
*
* // 获得当前时间 - 绝对时间
* CFTimeInterval end = CFAbsoluteTimeGetCurrent();
*
*
*/
-(void)setDownloadBtn:(UIButton *)clickBtn
{
[NSThread detachNewThreadSelector:@selector(downloadImg) toTarget:self withObject:nil];
}
-(void)downloadImg
{
// https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491485515021&di=b71bf37fbc13415e74dfe073a9fa6154&imgtype=0&src=http%3A%2F%2Ffun.youth.cn%2Fyl24xs%2F201608%2FW020160824572760273145.png
// 1. 确定URL
NSURL * url = [NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491485515021&di=b71bf37fbc13415e74dfe073a9fa6154&imgtype=0&src=http%3A%2F%2Ffun.youth.cn%2Fyl24xs%2F201608%2FW020160824572760273145.png"];
// 2. 根据URL下载图片 二进制数据 到本地
NSData * imageData = [NSData dataWithContentsOfURL:url];
// 3. 二进制数据 转换图片格式
UIImage * image = [UIImage imageWithData:imageData];
NSLog(@"download----%@",[NSThread currentThread]);
// 4. 回到主线程刷新显示UI
/*
第一参数: 回到主线程要调用哪个方法
第二参数: 前面的方法需要传递的参数,此处就是image
第三参数: 是否等待子线程完成
*/
// [self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:YES];
// 另外一种线程通信方法
// [self performSelector:@selector(showImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];
// 简单方法
[self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
NSLog(@"-----end------");
}
#pragma mark - 用Object - C获取当前时间
-(void)downloadImgObjectC
{
// 1. 确定URL
NSURL * url = [NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491485515021&di=b71bf37fbc13415e74dfe073a9fa6154&imgtype=0&src=http%3A%2F%2Ffun.youth.cn%2Fyl24xs%2F201608%2FW020160824572760273145.png"];
// 2. 根据URL下载图片 二进制数据 到本地
NSData * imageData = [NSData dataWithContentsOfURL:url];
// 获得当前的时间
NSDate * startDate = [NSDate date];
// 3. 二进制数据 转换图片格式
UIImage * image = [UIImage imageWithData:imageData];
// 获取结束时间
NSDate * endDate = [NSDate date];
NSLog(@"%f",[endDate timeIntervalSinceDate:startDate]);
// 4. 显示UI
self.imageView.image = image;
}
#pragma mark - 用C语言获取时间
-(void)downloadC
{
// 1. 确定URL
NSURL * url = [NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491485515021&di=b71bf37fbc13415e74dfe073a9fa6154&imgtype=0&src=http%3A%2F%2Ffun.youth.cn%2Fyl24xs%2F201608%2FW020160824572760273145.png"];
// 2. 根据URL下载图片 二进制数据 到本地
NSData * imageData = [NSData dataWithContentsOfURL:url];
// 用C语言获取当前时间
// 获得当前时间 - 绝对时间
CFTimeInterval start = CFAbsoluteTimeGetCurrent();
// 3. 二进制数据 转换图片格式
UIImage * image = [UIImage imageWithData:imageData];
// 获得当前时间 - 绝对时间
CFTimeInterval end = CFAbsoluteTimeGetCurrent();
NSLog(@"end - start = %f",end - start);
// 4. 显示UI
self.imageView.image = image;
}
#pragma mark - 刷新图片
-(void)showImage:(UIImage *)image
{
self.imageView.image = image;
NSLog(@"UI ----- %@",[NSThread currentThread]);
}
@end