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
一个正在运行的应用程序是一个进程,一个进程会默认开启一个主线程,但是在主线程中的操作是串行的,也就是当有多个任务同时需要完成的时候,是按照顺序一个个执行.因此,为了提高效率,会在进程中开启多个线程,每 ...
随机推荐
- 免费领取百度云盘2048G永久空间,永久离线下载特权
百度云盘和360云盘之间的竞争可谓争锋相对,前段时间,百度和360就网盘免费都采取了自己的措施,最终,360一锤定音,直接免费送36T,并且如果你超过了36T的容量,还可以自动免费扩容! 可参看文章: ...
- [转]svn 清理失败 (cleanup 失败) 的解决方法
转载网址:http://www.tuicool.com/articles/biy6na 今天svn遇到一个头疼的问题,最开始更新的时候失败了,因为有文件被锁住了.按照以往的操作,我对父目录进行clea ...
- ORA-12519: TNS:no appropriate service handler found 解决(转)
可能是数据库上当前的连接数目已经超过了它能够处理的最大值. select count(*) from v$process --当前的连接数 select value from v$parameter ...
- 你听说过哪些坑人 or 赚钱的歪点子?真是骗局太多,傻子都不够用啊!
又到了骗术揭秘时间了.各位看客您扶稳做好了,来给你深扒一些行业内幕了. 或者说一些投机的思路. 先提前写一些字,相信你们一会评论时候会用的上.直接复制,比较省力. “这年头傻逼太多,骗子明显不够用啊. ...
- js 获取指定日期
查询几天后的js代码,如果查询当天的日期 if($("input[name='startTime']").val()==""){ $("input[n ...
- canvas绘制二次贝塞尔曲线----演示二次贝塞尔四个参数的作用
canvas中绘制二次贝塞尔曲线的方法为ctx.quadraticCurveTo(x1,y1,x2,y2); 四个参数分别为两个控制点的坐标.开始点即当前canvas中目前的点,如果想从指定的点开始, ...
- 记一次程序排错与std::getline
今天忙活了半个下午,查找正式环境上面一个程序的问题.这个程序的作用是监控文件夹,处理每一个文件,分析每个文件的每行记录,然后将这个文件拆分成两个结果文件投放到另外两个不同的目录下面去,当处理完这个文件 ...
- IO口
STM32的每个IO端口都有7个寄存器来控制.他们是:CRH CRL IDR ODR BSRR BRR LCKR.我们常用的IO端口寄存器位CRL CRH IDR ODR.CRL CRH控制着每个IO ...
- RFID电子标签天线的印刷
RFID 电子标签技术又称RFID(Radio FrequencyIdentification)射频识别技术,是一种非接触式的自动识别技术,通过相距几厘米到几米距离内传感器发射的无线电波,可以读取RF ...
- source insight 编程风格(持续更新)
1.字体Source Code Pro 出身于豪门Adobe,从名字上来看就知道是转为编码而生的.基本上也是拥有前面所提的编程字体的所有要素的.这个字体基本上具有编程字体所需的所有要素:等宽.支持Cl ...