多线程(二)NSThread
2016-03-22 23:39
323 查看
一、NSThread优缺点
优点:NSThread是最轻量级的
缺点:需要自己管理线程的生命周期,线程同步。线程同步对数据的加锁会有一定的系统开销
二、NSThread的使用
创建线程:
+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(nullableid)argument;
- (instancetype)initWithTarget:(id)target selector:(SEL)selector object:(nullableid)argument; //argument:传输给target的唯一参数,也可以是nil
直接创建线程,并开始执行线程
[NSThread detachNewThreadSelector:@selector(doSomething:) toTarget:self withObject:nil];
先创建线程对象,然后再运行线程操作,在运行线程操作前可以设置线程的优先级等线程信息
NSThread* myThread = [[NSThread alloc] initWithTarget:self
selector:@selector(doSomething:)
object:nil];
[myThread start];
不显式创建线程的方法,Swift中去掉了这个方法,苹果认为 performSelector: 不安全,,,,,
NSObject *obj;
[obj performSelectorInBackground:@selector(doSomething) withObject:nil];
例子:
- (void)viewDidLoad {
[super viewDidLoad];
// [NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:kURL];
NSURL *url = [NSURL URLWithString:@"http://h.hiphotos.baidu.com/image/pic/item/5366d0160924ab1828b7c95336fae6cd7b890b34.jpg"];
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(downloadImage:) object:url];
[thread start];
}
- (void)downloadImage:(NSString *)url {
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *image = [[UIImage alloc]initWithData:data];
if(image == nil){
}else{
[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
}
}
- (void)updateUI:(UIImage *)image {
self.iconView.image = image;
}
更新其他线程
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait
常用属性
//取消线程
- (void)cancel;
//启动线程
- (void)start;
//判断某个线程的状态的属性
@property (readonly, getter=isExecuting) BOOL executing;
@property (readonly, getter=isFinished) BOOL finished;
@property (readonly, getter=isCancelled) BOOL cancelled;
//设置和获取线程名字
-(void)setName:(NSString *)n;
-(NSString *)name;
//获取当前线程信息
+ (NSThread *)currentThread;
//获取主线程信息
+ (NSThread *)mainThread;
//使当前线程暂停一段时间,或者暂停到某个时刻
+ (void)sleepForTimeInterval:(NSTimeInterval)time;
+ (void)sleepUntilDate:(NSDate *)date;
优点:NSThread是最轻量级的
缺点:需要自己管理线程的生命周期,线程同步。线程同步对数据的加锁会有一定的系统开销
二、NSThread的使用
创建线程:
+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(nullableid)argument;
- (instancetype)initWithTarget:(id)target selector:(SEL)selector object:(nullableid)argument; //argument:传输给target的唯一参数,也可以是nil
直接创建线程,并开始执行线程
[NSThread detachNewThreadSelector:@selector(doSomething:) toTarget:self withObject:nil];
先创建线程对象,然后再运行线程操作,在运行线程操作前可以设置线程的优先级等线程信息
NSThread* myThread = [[NSThread alloc] initWithTarget:self
selector:@selector(doSomething:)
object:nil];
[myThread start];
不显式创建线程的方法,Swift中去掉了这个方法,苹果认为 performSelector: 不安全,,,,,
NSObject *obj;
[obj performSelectorInBackground:@selector(doSomething) withObject:nil];
例子:
- (void)viewDidLoad {
[super viewDidLoad];
// [NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:kURL];
NSURL *url = [NSURL URLWithString:@"http://h.hiphotos.baidu.com/image/pic/item/5366d0160924ab1828b7c95336fae6cd7b890b34.jpg"];
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(downloadImage:) object:url];
[thread start];
}
- (void)downloadImage:(NSString *)url {
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *image = [[UIImage alloc]initWithData:data];
if(image == nil){
}else{
[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
}
}
- (void)updateUI:(UIImage *)image {
self.iconView.image = image;
}
更新其他线程
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait
常用属性
//取消线程
- (void)cancel;
//启动线程
- (void)start;
//判断某个线程的状态的属性
@property (readonly, getter=isExecuting) BOOL executing;
@property (readonly, getter=isFinished) BOOL finished;
@property (readonly, getter=isCancelled) BOOL cancelled;
//设置和获取线程名字
-(void)setName:(NSString *)n;
-(NSString *)name;
//获取当前线程信息
+ (NSThread *)currentThread;
//获取主线程信息
+ (NSThread *)mainThread;
//使当前线程暂停一段时间,或者暂停到某个时刻
+ (void)sleepForTimeInterval:(NSTimeInterval)time;
+ (void)sleepUntilDate:(NSDate *)date;
相关文章推荐
- 项目之IC卡程序2
- JVM内幕:Java虚拟机详解
- ASP.NET-ActionResutlt
- ASP.NET-MVC中Entity和Model之间的关系
- 14. Longest Common Prefix
- Linux常用命令/工具(一)
- 大龄程序员的未来在何方
- 自定义全局监听(任意位置任意行为监听)
- 学习JS的第三天
- 考试系统软件需求说明书(齐能、梁瑷云、李红、米军奉)
- HDU 4403 A very hard Aoshu problem(DFS)
- winform treeview绑定数据 DOM操作
- ASP.NET-viewBag Viewdata Tempdata
- hadoop2安装脚本
- 插入排序
- POJ2449
- jenkins(二)linux slave构建
- 8个方法让你成为更优秀的程序员
- 初始化一个新的cURL辉煌并抓取一个web页面。
- 在.NET Core中使用MEF