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
一个正在运行的应用程序是一个进程,一个进程会默认开启一个主线程,但是在主线程中的操作是串行的,也就是当有多个任务同时需要完成的时候,是按照顺序一个个执行.因此,为了提高效率,会在进程中开启多个线程,每 ...
随机推荐
- [Android]新版的sdk中新建一个android应用,增加的PlaceholderFragment这个静态类发生的事情
1,首先发生的是有两个布局xml,一个activity_main.xml,一个是fragment_main.xml一开始没在意,后来仔细看了原来是新功能的fragment概念等于多个场景在这个acti ...
- linux常用命令(二)
文件处理命令接上一篇 cat concatenate and display fils 查看小文件 more分页显示文件 空格或者f翻页,回车下一行,Q或q退出. head –num num代表行数 ...
- [转载] 自定义百度网盘分享密码 (Javascript)
压缩版 javascript:require(["function-widget-1:share/util/service/createLinkShare.js"]).protot ...
- Thinking in Java——笔记(18)
I/O The original byte-oriented library was supplemented with char-oriented, Unicode-based I/O classe ...
- Glide加载异常调试
Glide提供了listener链式调用方法,传入一个RequestListener对象,然后实现内部的onException方法,那么在Glide加载图片失败时就会调用这个方法. 代码实现很简单: ...
- Linux从零单排(一):Google Chrome的安装
刚刚安装了Linux ubuntu16.10系统,先装上我一直习惯用的Google Chrome 安装步骤如下: 1.终端输入 sudo wget https://repo.fdzh.org/chro ...
- centos 6.5安装node.js
1.检查是否安装gcc编译器 rpm -q gcc rpm -q gcc-c++ 2.如果没有安装则通过以下代码安装gcc编译器 yum -y install gcc-c++ kernel-devel ...
- C#开源系统大汇总(个人收藏)
C#开源系统大汇总 一.AOP框架 Encase 是C#编写开发的为.NET平台提供的AOP框架.Encase 独特的提供了把方面(aspects)部署到运行时代码,而其它AOP框架依赖 ...
- js实现下滑加载更多的效果
var stop=true;$(window).scroll(function(){ totalheight = parseFloat($(window).height()) + parseFloat ...
- css重置reset.css
body, h1, h2, h3, h4, h5, h6, hr, p, blockquote,dl, dt, dd, ul, ol, li,pre,form, fieldset, legend, b ...