iphone的多线程NSThread
用法例子:
不传参数的情况
[NSThread detachNewThreadSelector:@selector(test1) toTarget:self withObject:nil];
-(void) test1{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];//必须的 不然会造成内存泄漏
[pool release];
}
传参数的情况
[NSThread detachNewThreadSelector:@selector(test:) toTarget:self withObject:[NSDate date]];
-(void) test:(NSDate *) mdate {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//operator…
[pool release];
}
在线程里运行主线程里的方法:
[self performSelectorOnMainThread:@selector(moveToMain) withObject:nil waitUntilDone:FALSE];
参数介绍:
withObject:要传的参数
waitUntilDone:直到调用的函数返回才继续执行下面的wad
selector:回调的方法
toTarget:回调函数所在的实例或类(指静态函数)
本文详细介绍了如何使用iPhone的多线程机制NSThread,包括不传递参数和传递参数两种情况下的用法示例,并解释了在线程中运行主线程方法的方法。
167

被折叠的 条评论
为什么被折叠?



