1.在主线程执行多次NSLog模拟耗时操作

结果,卡住主线程

解决方案: performSelectorInBackground让程序在后台执行
 
2.pthread的使用
开辟子线程,执行一个函数
__bridge桥接,OC对象和C指针之间的转换
{  /*
参数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的更多相关文章

  1. iOS多线程 NSThread/GCD/NSOperationQueue

    无论是GCD,NSOperationQueue或是NSThread, 都没有线程安全 在需要同步的时候需要使用NSLock或者它的子类进行加锁同步 "] UTF8String], DISPA ...

  2. 多线程NSThread基本用法

        #import "ViewController.h" @interface ViewController () @end @implementation ViewContr ...

  3. [iOS]深入浅出 iOS 之多线程 NSThread

    OS 支持多个层次的多线程 编程,层次越高的抽象程度越高,使用起来也越方便,也是苹果最推荐使用的方法.     下面简要说明这三种不同范式:  Thread 是这三种范式里面相对轻量级的,但也是使用起 ...

  4. 多线程 -- NSThread

    NSThread NSThread 一个NSThread对象就代表一条线程 创建线程的几种方式 alloc/init // 1.创建线程 NJThread *thread = [[NJThread a ...

  5. [iOS 多线程 & 网络 - 1.1] - 多线程NSThread

    A.NSThread的基本使用 1.创建和启动线程 一个NSThread对象就代表一条线程创建.启动线程NSThread *thread = [[NSThread alloc] initWithTar ...

  6. 多线程 NSThread GCD

    ios多线程实现种类 NSThread NSOperationQueue NSObject GCD *************** 1.NSThread //线程 第一种 NSThread *thre ...

  7. iOS多线程NSThread和GCD

    在iOS中啊  其实有多种方法实现多线程 这里只记录两个比较常用的  或者说我比较常用的 一个就是BSThread 另一个就是一听名字就比较霸气的妇孺皆知的GCD 先说一下NSThread吧 这个方式 ...

  8. iOS 多线程NSThread理解与场景示例

    NSThread是相对GCD和NSOperationQuene而言,比较轻量级的一种多线程处理方式. 但同时,它的弊端就是需要自己管理线程的生命周期,以及线程同步:而另外两种不需要自己管理. 常见方法 ...

  9. 九、使用多线程——NSThread,GCD和NSOperation

    概述 早上起床,你先打开洗衣机,然后用热水把泡面泡上,接着打开电脑开启一天的码农生活.其中“洗衣服”.“泡泡面”和“码代码”3个任务(线程)同时进行,这就是多线程.网上有许多关于多线程的经典解释,此处 ...

  10. IOS 多线程 NSThread

    一个正在运行的应用程序是一个进程,一个进程会默认开启一个主线程,但是在主线程中的操作是串行的,也就是当有多个任务同时需要完成的时候,是按照顺序一个个执行.因此,为了提高效率,会在进程中开启多个线程,每 ...

随机推荐

  1. UI设计中的48dp定律【转】

    有朋友建议我偶尔写写技术类的文章,所以我打算开始穿插性的写一些偏技术方面的科普文章,尽量往小白能看懂的方向写,今天我来讲讲UI设计中的48dp定律. 那么先说说什么是dp ?其实对于一个非技术人员要把 ...

  2. Java并发编程底层实现原理 - volatile

    Java语言规范第三版中对volatile的定义如下: Java编程语言允许线程访问共享变量,为了确保共享变量能被准确和一致性的更新,线程应该确保通过排他锁 单独获得这个变量. volatile有时候 ...

  3. 泛型:HashMap的用法--输入字母输出数目

    public static void main(String[] args) { Map <String ,Integer> m =new HashMap<String , Inte ...

  4. 实战Java虚拟机之四:提升性能,禁用System.gc() ?

    今天开始实战Java虚拟机之四:"禁用System.gc()". 总计有5个系列 实战Java虚拟机之一“堆溢出处理” 实战Java虚拟机之二“虚拟机的工作模式” 实战Java虚拟 ...

  5. Eclipse下使用GDT插件无法登陆GAE & GDT无法上传JAVA代码

    今天更新github主页的过程中,想使用GAE部署一个Java Web服务来更好的支持网站动态性(关键是利用了免费的GAE资源),结果遇到了2个大问题. 1.GDT插件无法登陆GAE账户 错误1:登陆 ...

  6. Bootstrap的字体文件woff2 报错

    在iis上看到网站有个404,于是强迫症来了... 百度了许久,知道了解决办法,记下来,是网站默认没有支持这种字体 在 Web.config 的 system.webServer 节点中添加: 带标签 ...

  7. Spark java.lang.outofmemoryerror gc overhead limit exceeded 与 spark OOM:java heap space 解决方法

    引用自:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c42246 ...

  8. 20155206赵飞技能获取经验,C语言学习感想与对JAVA的学习目标

    自己较强的技能获取经验. 1:实话实说我自己是没有哪个技能可以超过90%的人的,只有自认为做的还可以的一些事情,例如打篮球,office软件的应用,一百米跑.至于其他方面就是很平庸了. 2:经验主要有 ...

  9. angularJs之http后台访问数据

    AngularJS  XMLHttpRequest $http  是AngularJS中的一个核心服务,用于读取远程服务器的数据. 读取JSON 文件 以下是存储在web服务器上的JSON 文件: h ...

  10. typealias和泛型接口

    typealias 是用来为已经存在的类型重新定义名字的,通过命名,可以使代码变得更加清晰.使用的语法也很简单,使用 typealias 关键字像使用普通的赋值语句一样,可以将某个已经存在的类型赋值为 ...