IOS 线程处理 子线程的启动与结束
2013-05-26 15:43
190 查看
IOS中,如果要在主线程中启动一个子线程,可以又两种方法:
[cpp]
[NSThread detachNewThreadSelector:@selector(myThreadMainMethod:) toTarget:self withObject:nil];
这是在cocoa早期提供的方法,因此你可以在任何版本的ios和mac上调用此方法。
在 OS X v10.5(or later)和IOS中,苹果又提供了一种方法,可以允许你获得你的thread句柄,并且更方便的让主线程控制子线程。
[cpp]
NSThread* myThread = [[NSThread alloc] initWithTarget:self
selector:@selector(myThreadMainMethod:)
object:nil];
[myThread start]; // Actually create the thread
如果要停止子线程,有两种方法:
第一种,是在子线程中执行:
[cpp]
[NSThread exit];
另一种是在主线程执行:
[cpp]
[myThread cancel];
要注意的是,[mThread cancel]; 并不能exit线程,只是标记为canceled,但线程并没有死掉。加入你在子线程中执行了一个循环,则cancel后,循环还在继续,你需要在循环的条件判断中加入 !mThread.isCancelled 来判断子线程是否已经被cancel来决定是否继续循环。
下面是我的一个测试demo,可以参考一下:
[cpp]
@synthesize mThread;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"main thread:%@",[NSThread currentThread]);
mThread=[[NSThread alloc] initWithTarget:self selector:@selector(subThreadMethod) object:nil];
[NSThread detachNewThreadSelector:@selector(performMethod) toTarget:self withObject:nil];
}
-(void)subThreadMethod{
int i=1;
while (i++>0 && ![[NSThread currentThread]isCancelled]) {
NSLog(@"subthread i:%d ,thread:%@",i,[NSThread currentThread]);
}
}
- (IBAction)startThread:(id)sender {
NSLog(@"startThread....");
[mThread start];
}
- (IBAction)stopThread:(id)sender {
NSLog(@"mThread.isCancelled: %d",mThread.isCancelled);
if (!mThread.isCancelled) {
[mThread cancel];
// [mThread exit]; //exit 是类方法,不可以用在对象上
}
}
- (IBAction)performOnSubThread:(id)sender {
//在子线程调用方法
[self performSelector:@selector(performMethod) onThread:mThread withObject:nil waitUntilDone:NO];
}
-(void)performMethod{
NSLog(@"performMethod.... thread:%@",[NSThread currentThread]);
}
@end
[cpp]
[NSThread detachNewThreadSelector:@selector(myThreadMainMethod:) toTarget:self withObject:nil];
这是在cocoa早期提供的方法,因此你可以在任何版本的ios和mac上调用此方法。
在 OS X v10.5(or later)和IOS中,苹果又提供了一种方法,可以允许你获得你的thread句柄,并且更方便的让主线程控制子线程。
[cpp]
NSThread* myThread = [[NSThread alloc] initWithTarget:self
selector:@selector(myThreadMainMethod:)
object:nil];
[myThread start]; // Actually create the thread
如果要停止子线程,有两种方法:
第一种,是在子线程中执行:
[cpp]
[NSThread exit];
另一种是在主线程执行:
[cpp]
[myThread cancel];
要注意的是,[mThread cancel]; 并不能exit线程,只是标记为canceled,但线程并没有死掉。加入你在子线程中执行了一个循环,则cancel后,循环还在继续,你需要在循环的条件判断中加入 !mThread.isCancelled 来判断子线程是否已经被cancel来决定是否继续循环。
下面是我的一个测试demo,可以参考一下:
[cpp]
@synthesize mThread;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"main thread:%@",[NSThread currentThread]);
mThread=[[NSThread alloc] initWithTarget:self selector:@selector(subThreadMethod) object:nil];
[NSThread detachNewThreadSelector:@selector(performMethod) toTarget:self withObject:nil];
}
-(void)subThreadMethod{
int i=1;
while (i++>0 && ![[NSThread currentThread]isCancelled]) {
NSLog(@"subthread i:%d ,thread:%@",i,[NSThread currentThread]);
}
}
- (IBAction)startThread:(id)sender {
NSLog(@"startThread....");
[mThread start];
}
- (IBAction)stopThread:(id)sender {
NSLog(@"mThread.isCancelled: %d",mThread.isCancelled);
if (!mThread.isCancelled) {
[mThread cancel];
// [mThread exit]; //exit 是类方法,不可以用在对象上
}
}
- (IBAction)performOnSubThread:(id)sender {
//在子线程调用方法
[self performSelector:@selector(performMethod) onThread:mThread withObject:nil waitUntilDone:NO];
}
-(void)performMethod{
NSLog(@"performMethod.... thread:%@",[NSThread currentThread]);
}
@end
相关文章推荐
- IOS 线程处理 子线程的启动与结束
- IOS 线程处理 子线程
- iOS多线程编程中,子线程如何访问和操作主线程UI中的视图控件(下载操作用子线程实现)
- App的启动过程(3)应用进程侧-主线程的处理
- IOS线程的并发与处理(翻译自:http://www.objc.io/issue-2/concurrency-apis-and-pitfalls.html)
- Junit测试含有‘启动新线程’这一操作的方法时瞬间结束的问题
- Java线程--在主线程和子线程中处理类变量
- 主线程等待子线程结束
- 非UI的子线程再启动线程的话,等待的时候会被阻塞,Task不会阻塞了
- CompletionService 创建多线程,并返回全部线程处理结束后的结果
- java线程池主线程等待子线程执行完成后再继续处理后面工作
- java 多线程 主线程等待子线程结束
- java 主线程等待子线程结束
- 执行主线程之后又执行了几个子线程,保证子线程执行完成在结束主线程
- Junit测试含有‘启动新线程’这一操作的方法时瞬间结束的问题
- ios 简化线程处理用NSOperationQueue
- 启动一个进程并处理进程结束事件
- iOS 如何创建一个线程,要求可以一直工作,不会执行一次就结束
- android使用ExecutorService来处理子线程与UI线程的通讯问题
- iOS 程序未启动和在前台、后台接收到推送消息的处理方法