iOS 多线程的四种技术方案
image
pthread 实现多线程操作
代码实现:
void * run(void *param)
{
for (NSInteger i = 0; i < 1000; i++) {
NSLog(@"---buttonclick---%zd---%@", i, [NSThread currentThread]);
}
return NULL;
}
@implementation ViewController
- (IBAction)clickButton:(id)sender {
// 定义一个线程
pthread_t thread;
// 创建一个线程 (参1)pthread_t *restrict:创建线程的指针,(参2)const pthread_attr_t *restrict:线程属性 (参3)void *(*)(void *):线程执行的函数的指针,(参4)void *restrict:null
pthread_create(&thread, NULL, run, NULL);
// 何时回收线程不需要你考虑
pthread_t thread2;
pthread_create(&thread2, NULL, run, NULL);
}复制代码
NSThread实现多线程
一个 NSThread 对象就代表一条线程
######创建线程的多种方式
- 第一种方式:先创建再启动线程
// 创建线程
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"jack"];
// 线程启动了,事情做完了才会死, 一个NSThread对象就代表一条线程
[thread start]; - 第二种:直接创建并启动线程
// 直接创建并启动线程
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"jack"]; - 第三种:
// 直接创建并启动线程
[self performSelectorInBackground:@selector(run:) withObject:@"jack"];
// 使线程进入阻塞状态
[NSThread sleepForTimeInterval:2.0];
#pragma mark - 执行run方法
- (void)run:(NSString *)param
{
// 当前线程是否是主线程
for (NSInteger i = 0; i < 100; i++) {
NSLog(@"---%@---%zd---%d", [NSThread currentThread], i, [NSThread isMainThread]);
}
}复制代码
方法2和方法3的优点:快捷
方法1的优点:可以轻松拿到线程
线程间通信
线程间通信的体现
1个线程传递数据给另1个线程
在1个线程中执行完特定任务后,转到另1个线程继续执行任务
线程间通信的常用方法:小程序图片下载
-(UIImageView *)imageView
{
if (!_imageView) {
_imageView = [UIImageView new];
_imageView.frame = CGRectMake(0, 0, 300, 300);
_imageView.center = self.view.center;
[self.view addSubview:_imageView];
}
return _imageView;
}
- (void)viewDidLoad {
[super viewDidLoad];
//监听线程结束的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handle_threadexit:) name:NSThreadWillExitNotification object:nil];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//第一种方式:先创建再启动线程
// 创建线程
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"booob"];
// 线程启动了,事情做完了才会死, 一个NSThread对象就代表一条线程
[thread start];
//第二种:直接创建并启动线程
// 直接创建并启动线程
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"wang"];
//第三种:
// 直接创建并启动线程
[self performSelectorInBackground:@selector(run:) withObject:@"wang000"];
// 使线程进入阻塞状态
[NSThread sleepForTimeInterval:2.0];
//例子
// 获取图片的url
NSURL *url = [NSURL URLWithString:@"https://pages.github.com/images/slideshow/yeoman.png"];
// 另开1条线程 object用于数据的传递
NSThread *thread3 = [[NSThread alloc] initWithTarget:self selector:@selector(downLoadWithURL:) object:url];
thread3.name = @"downloadimage...";
// 由于下面下载图片的耗时太长,应开启线程来完成
[thread3 start];
}
#pragma mark - 执行run方法
- (void)run:(NSString *)param
{
// 当前线程是否是主线程
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"---%@---%zd---%d", [NSThread currentThread], i, [NSThread isMainThread]);
}
}
//线程直接的交互
// 下载图片
- (void)downLoadWithURL:(NSURL *)url
{
NSLog(@"%s ,%s %@",__FILE__,__FUNCTION__, [NSThread currentThread]);
// 下载图片
NSData *data = [NSData dataWithContentsOfURL:url];
// 生成图片
UIImage *image = [UIImage imageWithData:data];
// 返回主线程显示图片
[self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
}
//处理线程结束事件
-(void)handle_threadexit:(NSNotification *)notify
{
NSThread * thread = (NSThread *)notify.object;
NSLog(@"+++++++++++++++ 线程 %@ 结束 ++++++++++++",thread.name);
}复制代码
TIPS: 拓展,线程结束的通知
以上下载图片方式使用线程已经过时了,开发中我们操作线程大多都使用 GCD 和 NSOperation 来实现多线程操作。
——————————————————————————————————————————
GCD 是如何实现多线程的
- GCD 实现多线程
- GCD 简介
-
GCD 全称是
Grand Central Dispatch
,可译为“超级厉害的中枢调度器”,GCD 是苹果公司为多核的并行运算提出的解决方案, GCD会自动利用更多的 CPU 内核(比如双核、四核)来开启线程执行任务,GCD 会自动管理线程的生命周期(创建线程、调度任务、销毁线程),不需要我们程序员手动管理内存。 -
任务和队列
- 任务:在同步函数和异步函数中执行
- 队列:用来存放任务(并发 串行)