多线程技术NSThread

 

NSThread:

优点:NSThread 比其他两个轻量级,使用简单

缺点:需要自己管理线程的生命周期、线程同步、加锁、睡眠以及唤醒等。线程同步对数据的加锁会有一定的系统开销

开启后台执行任务的方法

(void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg

在后台线程中通知主线程执行任务的方法

(void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;

获取线程信息

[NSThread currentThread];

线程休眠(仅适用于开发调试时使用)

[NSThread sleepForTimeInterval:1.0f];

 

创建线程方法:

+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)argument;

- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument;

参数说明:

selector:线程执行的方法,只能有一个参数,不能有返回值

target:selector消息发送的对象

argument:传输给target的唯一参数,也可以是nil

detachNewThreadSelector方法会直接启动线程方法

 initWithTarget需要调用start方法才能够启动线程方法

 

posted @ 2015-06-10 21:15  周圆民  阅读(129)  评论(0编辑  收藏  举报