Objective c 为什么’;我的后台线程没有运行吗?
没有从startDoingWork方法调用doBackgroundWork方法Objective c 为什么’;我的后台线程没有运行吗?,objective-c,Objective C,没有从startDoingWork方法调用doBackgroundWork方法 threading.h ------------ #import <Foundation/Foundation.h> @interface threading : NSObject { NSConditionLock* theConditionLock; NSMutableArray* workItems; } -(void
threading.h
------------
#import <Foundation/Foundation.h>
@interface threading : NSObject {
NSConditionLock* theConditionLock;
NSMutableArray* workItems;
}
-(void)startDoingWork;
-(void)doBackgroundWork;
-(void)notifyBackgroundThreadAboutNewWork;
@end
threading.m
-------------
#import "threading.h"
enum{
workToDoNow = 1,
workNotToDoNow = 0
};
@implementation threading
-(id)init{
if (self = [super init]) {
theConditionLock = [[NSConditionLock alloc]initWithCondition:workNotToDoNow];
workItems = [[NSMutableArray alloc]init];
}
return self;
}
-(void)startDoingWork{
[NSThread detachNewThreadSelector:@selector(doBackgroundWork) toTarget:self withObject:nil];
}
-(void)doBackgroundWork{
while (YES) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc]init];
NSArray* items = nil;
NSLog(@"Going to lock!!");
[theConditionLock lockWhenCondition:workToDoNow];
items = [NSArray arrayWithArray:workItems];
[workItems removeAllObjects];
[theConditionLock unlockWithCondition:workNotToDoNow];
NSLog(@"Finished the work!!");
[pool drain];
}
}
-(void)notifyBackgroundThreadAboutNewWork{
NSLog(@"Into the Background new work!!");
[theConditionLock lock];
[workItems addObject:@"Hello"];
[theConditionLock unlockWithCondition:workToDoNow];
NSLog(@"Finished and came out!!");
}
@end
main.m
------
#import <Foundation/Foundation.h>
#import "threading.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
threading* threads = [[threading alloc]init];
[threads notifyBackgroundThreadAboutNewWork];
[threads startDoingWork];
[pool drain];
return 0;
}
threading.h
------------
#进口
@接口线程:NSObject{
NSConditionLock*conditionlock;
NSMUTABLEARRY*工作项;
}
-(无效)开始工作;
-(无效)地基;
-(无效)通知BackgroundThread有关新工作的信息;
@结束
线程
-------------
#导入“threading.h”
枚举{
workToDoNow=1,
workNotToDoNow=0
};
@实现线程
-(id)init{
if(self=[super init]){
conditionlock=[[NSConditionLock alloc]initWithCondition:worknottodow];
工作项=[[NSMutableArray alloc]init];
}
回归自我;
}
-(无效)开始工作{
[NSThread detachNewThreadSelector:@selector(doBackgroundWork)to target:self with object:nil];
}
-(空)地基{
虽然(是){
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
NSArray*项目=无;
NSLog(@“即将锁定!!”);
[条件锁定时的条件锁定:工作到关闭];
items=[NSArray arrayWithArray:workItems];
[工作项删除所有对象];
[ConditionLock解锁条件:WorkNotToDown];
NSLog(@“完成工作!!”);
[泳池排水沟];
}
}
-(无效)通知BackgroundThreadAboutnewwork{
NSLog(@“进入后台新工作!!”);
[条件锁];
[工作项添加对象:@“你好”];
[条件锁解锁条件:工作待办];
NSLog(@“完成并发布!!”);
}
@结束
main.m
------
#进口
#导入“threading.h”
int main(int argc,const char*argv[]{
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
线程*threads=[[threading alloc]init];
[线程通知BackgroundThreadAboutNewWork];
[线程开始工作];
[泳池排水沟];
返回0;
}
detachNewThread:selector:toTarget:withObject方法在调试时不会被调用。我认为您的辅助线程选择器应该只使用一个参数。难道您的主线程结束得太快,后台线程没有机会启动吗?你知道,甚至在运行时设置并启动新线程之前,应用程序就完成了,所有线程都被终止/关闭/不管是什么。您可以将主线程休眠一秒钟,以查看后台线程是否开始运行。1)执行是否达到
[threads startdoongwork]
行?2) 控制台里有什么?3)你有没有考虑使用?@佐尔:是的,它达到了。当它打开detachNewThreadSelector时,它不会跳到doBackgroundWork线程上。它来自startDoingWork方法。我正在控制台中获取notifybackgroundThreadAboutNewWork方法的输出Hi Alvin,很高兴知道。见“获取运行时异常”:-)