0

我使用NSOperationQueue. 我在其中设置maxConcurrentOperationCount25. 即同时同时运行 25 个线程。

我正在使用 this 将块上传到服务器NSOperationQueue。因此,块被分配给前 25 个线程。满后NSOperationQueue,我想暂停分块读取部分,然后每当队列中的线程完成时,恢复分块部分以分配新线程NSOperationQueue以替换完成的线程。

我的代码:

NSOperationQueue *operationQueue = [NSOperationQueue new];
operationQueue.maxConcurrentOperationCount=5;

NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self                                                                         selector:@selector(upload:)                                                                            object:f_objChunkDetails->FileStream];

NSUInteger oprCnt=operationQueue.operationCount;

if(oprCnt >= 5) {
    // wait till queue has a free slot
} else {
    [operationQueue addOperation:operation];
}

那么如何使用暂停和恢复NSOperationQueue呢?如何ManualResetEvent在 Objective-C 中实现?

4

1 回答 1

0

不要等待或暂停。相反,将您的工作创造(和检查)转移到一种新方法中。该方法应该循环创建作业,直到可用限制,然后返回。创建的每个作业都应该completionBlock添加一个调用作业创建方法的方法。

通过这种方式,您是事件触发而不是阻塞。

一般来说,completionBlock在调用job创建方法之前应该先切换到主线程。

于 2014-03-19T15:33:03.557 回答