- (IBAction)nsop1:(id)sender {
//使用NSOperationQueue 可以用depend非常方便的控制线程之间的执行顺序
NSOperationQueue *q=[[NSOperationQueue alloc] init];
[q addOperationWithBlock:^{
int x=0;
while (x<20) {
NSLog(@"%@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:2];
//多线程间参数传递
[self performSelector:@selector(showInfo:) onThread:[NSThread mainThread] withObject:@"create-queue dispathc-syn" waitUntilDone:NO];
x++;
}
}];
}
- (IBAction)nsop2:(id)sender {
NSBlockOperation *b1=[[NSBlockOperation alloc]init];
[b1 addExecutionBlock:^{
NSLog(@"test NSBlockOperation %@",[NSThread currentThread]);
}];
NSBlockOperation *b2=[NSBlockOperation blockOperationWithBlock:^{
NSLog(@"test BSBlockOperation blockOperationWithBlock! %@",[NSThread currentThread]);
}];
NSOperationQueue *q=[[NSOperationQueue alloc] init];
[q addOperation:b1];
[q addOperation:b2];
}
- (IBAction)nsop3:(id)sender {
NSBlockOperation *b1=[[NSBlockOperation alloc]init];
[b1 addExecutionBlock:^{
NSLog(@"test NSBlockOperation %@",[NSThread currentThread]);
}];
NSBlockOperation *b2=[NSBlockOperation blockOperationWithBlock:^{
NSLog(@"test BSBlockOperation blockOperationWithBlock! %@",[NSThread currentThread]);
}];
NSOperationQueue *q=[[NSOperationQueue alloc] init];
q.maxConcurrentOperationCount=4;
[b1 addDependency:b2];
[q addOperation:b1];
[q addOperation:b2];
}