完成如何使backgroundthread不工作,直到另一个后台线程被完成,如何使其启动它的线程一旦第一backgroundthread被完成制作后台线程等待,直到其他后台线程获取iphone
回答
使用标志处理此类事件的类型,如下所示...
BOOL isYourthreadexecuting = NO;
- (void)beginThread {
isYourthreadexecuting = YES;
[self performSelectorInBackground:@selector(backgroundThread) withObject:nil];
}
- (void)backgroundThread {
[myClass performLongTask];
// Done!
isYourthreadexecuting = NO;
}
- (void)waitForThread {
if (! isYourthreadexecuting) {
// Thread completed
[self callyourmethod];
}
}
编辑>>加成根据使用评论
我建议你使用NSOperationQueue用于多线程。
希望,这将你...
现在我正在遵循以下过程在你的答案中提到,但问题是第二个后台线程可能会开始任何时刻,它可能从其他类的方法也开始 – user1184202
看到我编辑的答案.. – Nit
@ user1184202如果你有你的答案,它会帮助你,然后标记它正确对其他人有帮助,对你也有帮助 – vishiphone
正如我在评论说,你可以使用GCD的串行调度队列。这里是一个示例代码来演示:
- (IBAction)buttonSerialQ2Pressed:(id)sender
{
dispatch_queue_t serialdQueue;
serialdQueue = dispatch_queue_create("com.mydomain.testbed.serialQ2", NULL);
dispatch_async(serialdQueue, ^{
//your code here
[self method1];
});
dispatch_async(serialdQueue, ^{
//your code here
[self method2];
});
dispatch_async(serialdQueue, ^{
//your code here
[self method2];
});
dispatch_async(serialdQueue, ^{
//your code here
[self method3];
});
}
-(void)method1
{
for (int i=0; i<1000; i++)
{
NSLog(@"method1 i: %i", i);
}
}
-(void)method2
{
for (int i=0; i<10; i++)
{
NSLog(@"method2 i: %i", i);
}
}
-(void)method3
{
for (int i=0; i<100; i++)
{
NSLog(@"method3 i: %i", i);
}
}
- 1. 请等待后台线程?
- 2. 好吧,从其他后台线程启动后台线程? (NSObj)
- 3. 等待多个后台线程
- 4. 如何让EDT等待后台线程?
- 5. UI线程如何等待直到后台线程完成处理?
- 6. (iphone)pushViewController在后台线程?
- 7. 主线程正在等待更新接口的后台线程
- 8. 如何让UI线程等待后台线程完成?
- 9. 主线程在后台线程执行时等待[NSManagedObjectContext(_NSInternalAdditions)lockObjectStore]
- 10. 后台线程
- 11. 后台线程
- 12. 前台线程VS后台线程
- 13. 从后台线程在UI线程中编程创建UI并将其复制到后台线程?
- 14. 后台工作线程
- 15. ASP.NET后台工作线程
- 16. C#等待其他线程
- 17. 我可以等待一个特定的后台线程完成,然后另一个后台线程启动?
- 18. Android:后台线程
- 19. KVO后台线程
- 20. tomcat后台线程
- 21. Java后台线程
- 22. 从后台线程
- 23. iPhone SDK后台线程调用其他方法
- 24. NSFetchedResultsController:在后台线程中获取
- 25. 从ALAssetRepresentation获取fullScreenImage在后台线程
- 26. 当后台线程完成其工作时获取响应
- 27. 从后台处理程序线程传递处理程序到后台线程
- 28. 强制SKPaymentQueue到在后台线程
- 29. 后台线程和连接将使线程为前台线程
- 30. 主线程是前台线程还是后台线程?
kindle请帮助我,如果你知道答案 – user1184202
我已经写了一些代码使用它。对于更多的帮助,你应该在这里显示一些代码,所以我可以理解场景...享受.. – Nit
可能需要使用串行调度队列。 https://developer.apple.com/library/mac/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW1 – user523234