222,NSThread类的使用
2016-01-20 13:11
239 查看
1,创建线程
(1)使用NSThread类中initWithTarget:创建线程
创建后,需要手动启动线程NSThread *thread1 = [[NSThreadalloc]
initWithTarget:selfselector:@selector(run:)object:@"http:www.baidu.com"];
//启动线程
[thread1 start]
(2)使用NSThread类中detachNewThreadSelector:创建线程
创建线程后,自动启动线程[NSThreaddetachNewThreadSelector:@selector(run:)toTarget:selfwithObject:@"http://www.hao123.com"];
(3)隐形创建线程
创建线程后,自动启动线程[selfperformSelectorInBackground:@selector(run:)withObject:@"http://www.google.com"];
- (void)run:(NSString *)url{
NSLog(@"-----%@-----%@-----%@",[NSThreadcurrentThread],[NSThreadmainThread],url);
}
2,NSThread的常用属性与方法
(1)获取当前线程
+ (NSThread *)currentThread;(2)获取主线程
+ (NSThread *)mainThread;(3)退出线程
+ (void)exit;(4)线程睡眠
+ (void)sleepUntilDate:(NSDate *)date;+ (void)sleepForTimeInterval:(NSTimeInterval)ti;
eg:
//睡眠五秒,线程阻塞五秒
[NSThreadsleepForTimeInterval:5];
或
NSDate *date = [NSDatedateWithTimeIntervalSinceNow:5];
[NSThreadsleepUntilDate:date];
(5)设置线程的优先级
+ (double)threadPriority;+ (BOOL)setThreadPriority:(double)p;
范围为0-1,默认情况下,优先级为0.5,数值越大,线程越先被调用
@property double threadPriority
(6)设置线程的名称
@property (nullable,copy)NSString *name
(7)是不是主线程
+ (BOOL)isMainThread3,线程同步
(1)实质:为了防止多个线程抢夺同一个资源造成的数据安全问题(2)解决方案:给代码加一个互斥锁(同步锁)
互斥锁使用格式
@synchronized(锁对象) {//
需要锁定的代码 }
注意:锁定1份代码只用1把锁,用多把锁是无效的
eg:
#define myStrong(name) @property (nonatomic,strong) NSThread *name;
#import "ViewController.h"
@interface
ViewController ()
@property (nonatomic,assign)
int ticketCount;
myStrong(thread1);
myStrong(thread2);
myStrong(thread3);
@end
@implementation ViewController
- (void)viewDidLoad{
self.ticketCount =
100;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
self.thread1 = [[NSThread
alloc] initWithTarget:self
selector:@selector(buyTicket)
object:nil];
self.thread1.name =
@"thread1";
self.thread2 = [[NSThread
alloc] initWithTarget:self
selector:@selector(buyTicket)
object:nil];
self.thread2.name =
@"thread2";
self.thread3 = [[NSThread
alloc] initWithTarget:self
selector:@selector(buyTicket)
object:nil];
self.thread3.name =
@"thread3";
[self.thread1
start];
[self.thread2
start];
[self.thread3
start];
}
- (void)buyTicket{
[NSThread
sleepForTimeInterval:0.2];
while (1) {
@synchronized(self){
int count =
self.ticketCount;
if (count >
0) {
self.ticketCount = count -
1;
NSLog(@"当前线程的名字为%@,卖出一张,剩余的票
%i",[NSThread
currentThread].name,self.ticketCount);
}else{
return;
}
}}
}
@end
4,原子属性与非原子属性
nonatomic和atomic对比atomic:线程安全,需要消耗大量的资源
nonatomic:非线程安全,适合内存小的移动设备
iOS开发的建议
所有属性都声明为nonatomic
尽量避免多线程抢夺同一块资源
尽量将加锁、资源抢夺的业务逻辑交给服务器端处理,减小移动客户端的压力
注:线程执行完就死亡了,不能再次start;若要重新执行同样的操作,则需要重新创建线程对象。以下为线程生命周期,
相关文章推荐
- Java 原子操作与并发
- httpClient访问网络,httpclient.execute(httpGet)方法不执行问题
- 微博即将取消140字发布限制
- windows下面读取注册表信息
- Spring 定时调度任务
- ServiceStack.OrmLite 学习笔记3 建表
- diff
- 分辨率与超分辨率图像重建
- Educational Codeforces Round 3 D.Gadgets for dollars and pounds(贪心&&二分)
- BZOJ 2879: [Noi2012]美食节( 费用流 + 动态加边 )
- cod pods安装过程中Error fetching http://ruby.taobao.org/: bad response Not Found 404 (http://ruby.taobao
- Javascript面向对象编程(三):非构造函数的继承
- 华为8年女硕离职:多么痛的领悟...
- Java:String和Date、Timestamp之间的转换
- 2016.1.20新技术学习记录
- apache无法启动解决办法
- .net邮件错误 :The specified string is not in the form required for a subject.
- fragment+ViewPager的使用(页面的滑动切换)
- Git版本库规范
- 前端的注意事项