1.多线程-NSThread
1.在主线程执行多次NSLog模拟耗时操作
结果,卡住主线程
{ /*
参数1:线程的编号(地址)
参数2:线程的属性 NULL nil(oc)
参数3:要调用的函数 void * (*) (void *)
参数4:给要调用的函数传递的参数
*/
//开辟新的线程
pthread_t ID;
NSString *str = @"ls";
//__bridge桥接 类型转换(oc - 》c)
//MRC 谁创建,谁释放
//ARC 自动管理
int result = pthread_create(&ID, NULL, demo, (__bridge void *)(str));
//result 0 代表成功 其他代表失败
if (result == ) {
NSLog(@"成功");
}else
{
NSLog(@"失败");
} }
/**
pthread调用的函数
*/
void * demo(void * param)
{
NSString *Str = (__bridge NSString *)(param);
//获取当前的代码执行在哪个线程当中,获取当前线程
NSLog(@"%@ %@",Str,[NSThread currentThread]);
return NULL;
}
3.NSThread的3种使用方式
{ //创建
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(demo) object:nil];
//调用
[thread start]; // //方式2
[NSThread detachNewThreadSelector:@selector(demo) toTarget:self withObject:nil]; //方式3
[self performSelectorInBackground:@selector(demo) withObject:nil]; //传递参数
[self performSelectorInBackground:@selector(demo2:) withObject:@"HM"]; }
- (void)demo2:(NSString *)str
{
NSLog(@"%@ %@",str,[NSThread currentThread]);
}
/**
要调用的方法
*/
- (void)demo
{
NSLog(@"%@",[NSThread currentThread]);
}
4.线程的生命周期
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//新建状态
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(demo) object:nil];
//就绪状态
[thread start]; //运行状态:系统控制的 }
- (void)demo
{
for (int i = ; i <; i++) { if (i == ) {
//阻塞状态
//让当前的线程睡一段时间
[NSThread sleepForTimeInterval:];
}else if (i == )
{
//死亡状态
[NSThread exit];
}
NSLog(@"%d",i);
}
}
5.NSThread的属性,可以设置属性的名称、优先级
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//线程属性
//主线程占用的内存空间
NSLog(@"%zd",[NSThread currentThread].stackSize/);
//创建线程1
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(demo) object:nil];
//设置线程名称
thread.name = @"t1";
//设置线程的优先级 参数的取值范围 0-1 0是优先级最低的,1是优先级最高 默认的是0.5
[thread setThreadPriority:1.0];
[thread start]; //创建线程2
NSThread *thread2 = [[NSThread alloc]initWithTarget:self selector:@selector(demo) object:nil];
//设置线程名称
thread2.name = @"t2";
[thread2 setThreadPriority:0.0];
[thread2 start]; //设置线程的优先级不能绝对保证线程优先执行,但是线程被调用的概率提高 }
- (void)demo
{ //子线程占用的内存空间
NSLog(@"demo %zd",[NSThread currentThread].stackSize/);
// NSLog(@"%@",[NSThread currentThread]);
for (int i = ; i < ; i++) {
NSLog(@"%d %@",i,[NSThread currentThread].name); } }
6.线程间资源抢夺的时候,需要加互斥锁(锁住一个对象)
#import "ViewController.h" @interface ViewController ()
//总票数
@property(nonatomic,assign)int totalTickets;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
//设置票数为20
self.totalTickets = ; }
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//窗口1 模拟卖票
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil];
thread.name = @"t1";
[thread start]; //窗口2 模拟卖票 NSThread *thread2 = [[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil];
thread2.name = @"t2";
[thread2 start];
}
/**
卖票
*/
- (void)sellTickets
{
while (YES) {
//被加锁的对象
@synchronized(self) {
//查询剩余的票数,判断
if (self.totalTickets > ) {
self.totalTickets = self.totalTickets - ;
NSLog(@"剩余%d票 %@",self.totalTickets,[NSThread currentThread].name);
}else
{
NSLog(@"票卖完了,回不了家,走路回家");
break;
}
} }
}
7.异步下载图片,在子线程下载图片,在主线程更新UI
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//网络图片下载
/*
1.不能把耗时操作放到主线程中,开辟新的线程
2.刷新ui一定要在主线程
// http://g.hiphotos.baidu.com/image/pic/item/f31fbe096b63f624cd2991e98344ebf81b4ca3e0.jpg
*/
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(downloadImg) object:nil];
[thread start]; }
/**
下载图片
*/
- (void)downloadImg
{
NSLog(@"downloadImg %@",[NSThread currentThread]);
//获取链接地址
NSURL *url = [NSURL URLWithString:@"http://g.hiphotos.baidu.com/image/pic/item/f31fbe096b63f624cd2991e98344ebf81b4ca3e0.jpg"];
//转化NSData类型
NSData *data = [NSData dataWithContentsOfURL:url];
//转化成UIImage
UIImage *img = [UIImage imageWithData:data]; /*
参数1:主线程要调用的方法
参数2:给调用的方法传递的参数
参数3:是否等待当前代码执行完毕再来执行下面的代码
*/
[self performSelectorOnMainThread:@selector(updataUI:) withObject:img waitUntilDone:YES];
NSLog(@"end"); }
/**
刷新ui ui刷新必须放到主线程里面
*/
- (void)updataUI:(UIImage *)img
{
NSLog(@"updataUI %@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:];
self.HMImageView.image = img;
}
1.多线程-NSThread的更多相关文章
- iOS多线程 NSThread/GCD/NSOperationQueue
无论是GCD,NSOperationQueue或是NSThread, 都没有线程安全 在需要同步的时候需要使用NSLock或者它的子类进行加锁同步 "] UTF8String], DISPA ...
- 多线程NSThread基本用法
#import "ViewController.h" @interface ViewController () @end @implementation ViewContr ...
- [iOS]深入浅出 iOS 之多线程 NSThread
OS 支持多个层次的多线程 编程,层次越高的抽象程度越高,使用起来也越方便,也是苹果最推荐使用的方法. 下面简要说明这三种不同范式: Thread 是这三种范式里面相对轻量级的,但也是使用起 ...
- 多线程 -- NSThread
NSThread NSThread 一个NSThread对象就代表一条线程 创建线程的几种方式 alloc/init // 1.创建线程 NJThread *thread = [[NJThread a ...
- [iOS 多线程 & 网络 - 1.1] - 多线程NSThread
A.NSThread的基本使用 1.创建和启动线程 一个NSThread对象就代表一条线程创建.启动线程NSThread *thread = [[NSThread alloc] initWithTar ...
- 多线程 NSThread GCD
ios多线程实现种类 NSThread NSOperationQueue NSObject GCD *************** 1.NSThread //线程 第一种 NSThread *thre ...
- iOS多线程NSThread和GCD
在iOS中啊 其实有多种方法实现多线程 这里只记录两个比较常用的 或者说我比较常用的 一个就是BSThread 另一个就是一听名字就比较霸气的妇孺皆知的GCD 先说一下NSThread吧 这个方式 ...
- iOS 多线程NSThread理解与场景示例
NSThread是相对GCD和NSOperationQuene而言,比较轻量级的一种多线程处理方式. 但同时,它的弊端就是需要自己管理线程的生命周期,以及线程同步:而另外两种不需要自己管理. 常见方法 ...
- 九、使用多线程——NSThread,GCD和NSOperation
概述 早上起床,你先打开洗衣机,然后用热水把泡面泡上,接着打开电脑开启一天的码农生活.其中“洗衣服”.“泡泡面”和“码代码”3个任务(线程)同时进行,这就是多线程.网上有许多关于多线程的经典解释,此处 ...
- IOS 多线程 NSThread
一个正在运行的应用程序是一个进程,一个进程会默认开启一个主线程,但是在主线程中的操作是串行的,也就是当有多个任务同时需要完成的时候,是按照顺序一个个执行.因此,为了提高效率,会在进程中开启多个线程,每 ...
随机推荐
- 工作中的sql语句总结
1,查找mysql 数据库 自动 添加 序号 字段列1,2,3,4 ) AS rowno,ip,startcount ) b 2,mysql的分页语句 limit后面第一个参数是index,从0开始: ...
- Python开发: DOM
文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM把 ...
- Android 上传图片并添加参数 PHP接收
php端接收代码: public function get_file(){ $local_path = "./Public/daixu_picture/figure/";//服务器 ...
- php开发常见问题
ajax 方面: ajax写法: //简写版ajax$.get('url.php',{'name':'myname','age':'18'},function(data){},'json'); ...
- 20145205 java语言实现数据结构实验一
数据结构实验要求 综合类实验设计3 已知有一组数据a1a2a3a4--anb1b2b3b4--bm,其中ai均大于bj,但是a1到an和b1到bm不是有序的,试设计两到三个算法完成数据排序,且把bj数 ...
- PHPExcel中文开发手册翻译版(2)
2016年8月18日12:45:14 请注意这个是粗翻译版,仅供参考,不是精校版 精校版后面才会更新 PHPExcel开发者文档 1.目录 2. 4先决条件 2.1.软件要求4 2.2.安装说明4 2 ...
- Eclipse修改编码格式
♣修改工作空间默认编码 ♣修改文件的编码 ♣修改某文件类型的编码 ♣修改JSP文件类型的编码 1.修改工作空间默认编码 window -> preferences -> General ...
- Servlet中的常用类以及常用方法
A:ServletConfig:用于读取Servlet在web.xml中配置的一些信息. getServletName(); getInitParameter();只能是Servlet自身下的参数设置 ...
- IntelliJ IDEA中如何显示和关闭----工具栏,目录栏,(转)
工具栏:就是上面有个好多的快捷按钮的那个栏,比如撤销,上传,下载设置,扳手按钮,等等. 目录栏,就是刚刚装的时候,这个会显示,但是不知道怎么关闭,但是这个又没什么用. 如图: 就是对应的几个按钮,自己 ...
- 带你玩转JavaWeb开发之五-如何完成响应式开发页面
响应式页面开发 使用BootStrap开发一个响应式的页面出来 响应式开发就是同一个页面在PC端与手机端Pad端显示不同的效果,以给用户更好的体验 需求分析 开发一套页面,让用户能够在PC端, Pad ...