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:回调函数所在的实例或类(指静态函数)