IOS开发中多线程的使用

news/2024/7/3 1:16:58
一、创建多线程的五种方式

1.开启线程的方法一

    NSThread * thread=[[NSThread alloc] initWithTarget:self selector:@selector(_update) object:nil];

2.开启线程的方法二 

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

3.开启线程的方法三

    [self performSelectorInBackground:@selector(_update) withObject:nil];

4.开启线程的方法四

    NSOperationQueue *queue=[[NSOperationQueue alloc] init];[queue addOperationWithBlock:^{for(int i=0;i<50;i++){printf("子线程\n");}}];

5.开启线程的方法五

    //第一步开启线程池NSOperationQueue * queue=[[NSOperationQueue alloc] init];//设置并发数目[queue setMaxConcurrentOperationCount:2];//第二部创建多线程添加到线程池NSInvocationOperation * thread1=[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(_update1) object:nil];NSInvocationOperation *thread2=[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(_update2) object:nil];[thread1 setQueuePriority:NSOperationQueuePriorityVeryLow];[thread2 setQueuePriority:NSOperationQueuePriorityVeryHigh];[queue addOperation:thread1];[queue addOperation:thread2];
二、多线程应用实例,加载图片。

1.核心思想

  考虑到如果加载网络图片会延迟,在一个主线程加载会影响控件的渲染,此时可以采取多线程,异步加载完成后刷新UI。

2.实现思路

  通过为UIImageView 增加类目来实现多线程下载。

  主要代码:

#import "UIImageView+thread.h"@implementation UIImageView(load)- (void) setImageWithUrl:(NSString *)url{[self performSelectorInBackground:@selector(_loadImage:) withObject:url];}- (void) _loadImage:(NSString *)u{@autoreleasepool {NSURL *url=[NSURL URLWithString:u];NSData *data=[NSData dataWithContentsOfURL:url];UIImage *image=[UIImage imageWithData:data];[self performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];}
}

 

作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/ 
本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

http://lihuaxi.xjx100.cn/news/245423.html

相关文章

Razor:从aspx到cshtml常见错误及正确书写方法

http://blog.csdn.net/cheny_com/article/details/6298496 从aspx转到chshtml还是有很多要适应的地方的&#xff0c;本帖是个人学习笔记帖不断更新。每天开着本帖编程。 按第一个有意义的编译错误的首字母排序&#xff0c;便于查找&#xff1a; Cannot implicitly convert type…

c语言读文件一行为一个数组元素,c语言数组练习习题(14页)-原创力文档

第 7章 数组习题A 卷1.单项选择题(1) int a[4]{5,3,8,9};其中a[3]的值为 (D )。DA.5B.3C.8D.9以下 4 个字符串函数中&#xff0c; ( A) 所在的头文件与其他 3 个不同。A. gets B. strcpy C. strlen D. strcmp以下 4 个数组定义中&#xff0c; ( D) 是错误的。 DA. int a[7]; B.…

手机上有android,android-在不同智能手机上的Videoview行为(具有...

使用videoview(或MediaPlayer)在同一活动中的多个videoview中播放rtsp流时遇到兼容性问题.我已经对here这个问题提出了另一个问题,但是现在我知道这不是我的代码负责,因为我在不同的手机上测试了相同的软件,并且可以正常工作.查看用于RTSP设置的logcat Info消息,我发现每部手机…

[Django](1093, quot;You can#39;t specify target table #39;fee_details_invoices#39; for update in...

dele_id Fee_details_invoices.objects.filter(fee_detail_id__infee_id_list, return_type2).values_list(fee_detail_id, flatTrue) Fee_details_invoices.objects.filter(fee_detail_id__indele_id).delete() 报错&#xff1a; django.db.utils.DatabaseError: (1093, &quo…

vmrun 批量创建vmware虚拟机

1 准备模板机 具体步骤如下&#xff1a; 1. 下载镜像安装系统 https://mirrors.aliyun.com/centos/7.5.1804/isos/x86_64/2. 安装完成配置好IP &#xff0c;关闭SELINUX ,关闭firewalld ,修改网卡名 3. 预设置好修改其他机器IP脚本 1.1 安装系统 略 1.2 模板机的设置 修改网卡名…

嵌入式开发之信号采集同步---VSYNC和HSYNC的作用以及它们两者之间的关系

VSYNC和HSYNC的作用以及它们两者之间的关系 VSYNC和HSYNC的作用以及它们两者之间的关系 VSYNC和HSYNC是什么 VSYNC: vertical synchronization&#xff0c;指与显示器的帧数同步。 简单来说就是启用了VSYNC的渲染过程&#xff0c;帧数不会超过显示器的帧数&#xff0c;一个同步…

力邀安卓为鸿蒙效力,期末阅读题答题秘籍2

原标题&#xff1a;期末阅读题答题秘籍2二、答题的步骤细解看了上次的顺口溜你不知道是否明白了一切该怎么做&#xff1f;顺口溜是帮助大家记忆的&#xff0c;毕竟语言过简&#xff0c;为了帮助大家很好答题&#xff0c;我还是详细解释一下吧。答阅读题的第一步&#xff1a;先读…

【TeeChart Pro ActiveX教程】(八):ADO数据库访问(上)

2019独角兽企业重金招聘Python工程师标准>>> 下载TeeChart Pro ActiveX最新版本 介绍 将TeeChart控件连接到ADO.NET数据库可以在设计时使用TeeChart编辑器完成&#xff0c;并在运行时使用几行代码完成。 任何Series都可以使用TeeChart Editor连接到ADO.NET表或查询。…