iOS多线程
来源:互联网 发布:淘宝宝贝关键词优化 编辑:程序博客网 时间:2024/05/16 07:05
- (void)updateLabel:(NSNumber *)aObject
{
self.displayLabel.text = [aObject stringValue];
NSLog(@"is duo xiancheng = %@",[[NSNumber numberWithBool:[NSThread isMultiThreaded]] stringValue]);
}
int n = 200000000;
- (void)compute:(NSNumber *)aObject
{
double sum = 0;
for (int a = aObject.intValue; a < n ; a++)
{
sum = sum + a;
// [self print:a];
}
[self performSelectorOnMainThread:@selector(updateLabel:) withObject:[NSNumber numberWithDouble:sum] waitUntilDone:NO];
}
- (void)thread1
{
[NSThread detachNewThreadSelector:@selector(compute:) toTarget:self withObject:[NSNumber numberWithInt:i]];
}
- (void)thread2
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//group
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
double sum = 0;
for (int a = i; a < n ; a++)
{
sum = sum + a;
}
NSLog(@"queue 1 = %f",sum);
});
dispatch_barrier_async(queue, ^{
double sum = 0;
for (int a = i; a < n ; a++)
{
sum = sum + a;
}
NSLog(@"barrier = %f",sum);
});
dispatch_group_async(group, queue, ^{
double sum = 0;
for (int a = i; a < n ; a++)
{
sum = sum + a;
}
NSLog(@"queue 2 = %f",sum);
});
dispatch_group_async(group, queue, ^{
double sum = 0;
for (int a = i; a < n ; a++)
{
sum = sum + a;
}
NSLog(@"queue 3 = %f",sum);
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
self.displayLabel.text = @"all done";
});
//async
// dispatch_async(queue, ^{
// double sum = 0;
// for (int a = i; a < n ; a++)
// {
// sum = sum + a;
//// [self print:a];
// }
// dispatch_async(dispatch_get_main_queue(), ^{
// self.displayLabel.text = [NSString stringWithFormat:@"%f",sum];
// NSLog(@"is duo xiancheng = %@",[[NSNumber numberWithBool:[NSThread isMultiThreaded]] stringValue]);
// });
// });
//
dispatch_async(queue, ^{
double sum = 0;
for (int a = i; a < n ; a++)
{
sum = sum + a;
}
NSLog(@"barrier queue 1 = %f",sum);
});
dispatch_async(queue, ^{
double sum = 0;
for (int a = i; a < n ; a++)
{
sum = sum + a;
}
NSLog(@"barrier = %f",sum);
});
dispatch_async(queue, ^{
double sum = 0;
for (int a = i; a < n ; a++)
{
sum = sum + a;
}
NSLog(@"barrier queue 2 = %f",sum);
});
dispatch_async(queue, ^{
double sum = 0;
for (int a = i; a < n ; a++)
{
sum = sum + a;
}
NSLog(@"barrier queue 3 = %f",sum);
});
}
- (void)thread3
{
NSMethodSignature *sig = [self methodSignatureForSelector:@selector(compute:)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
[invocation setTarget:self];
[invocation setSelector:@selector(compute:)];
NSNumber *num = [NSNumber numberWithInt:i];
[invocation setArgument:&num atIndex:2];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithInvocation:invocation];
//[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(compute:) object:];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];
}
- (void)thread4
{
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(compute:) object:[NSNumber numberWithInt:i]];
[thread start];
}
- (void)thread5
{
[self performSelectorInBackground:@selector(compute:) withObject:[NSNumber numberWithInt:i]];
}
{
self.displayLabel.text = [aObject stringValue];
NSLog(@"is duo xiancheng = %@",[[NSNumber numberWithBool:[NSThread isMultiThreaded]] stringValue]);
}
int n = 200000000;
- (void)compute:(NSNumber *)aObject
{
double sum = 0;
for (int a = aObject.intValue; a < n ; a++)
{
sum = sum + a;
// [self print:a];
}
[self performSelectorOnMainThread:@selector(updateLabel:) withObject:[NSNumber numberWithDouble:sum] waitUntilDone:NO];
}
- (void)thread1
{
[NSThread detachNewThreadSelector:@selector(compute:) toTarget:self withObject:[NSNumber numberWithInt:i]];
}
- (void)thread2
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//group
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
double sum = 0;
for (int a = i; a < n ; a++)
{
sum = sum + a;
}
NSLog(@"queue 1 = %f",sum);
});
dispatch_barrier_async(queue, ^{
double sum = 0;
for (int a = i; a < n ; a++)
{
sum = sum + a;
}
NSLog(@"barrier = %f",sum);
});
dispatch_group_async(group, queue, ^{
double sum = 0;
for (int a = i; a < n ; a++)
{
sum = sum + a;
}
NSLog(@"queue 2 = %f",sum);
});
dispatch_group_async(group, queue, ^{
double sum = 0;
for (int a = i; a < n ; a++)
{
sum = sum + a;
}
NSLog(@"queue 3 = %f",sum);
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
self.displayLabel.text = @"all done";
});
//async
// dispatch_async(queue, ^{
// double sum = 0;
// for (int a = i; a < n ; a++)
// {
// sum = sum + a;
//// [self print:a];
// }
// dispatch_async(dispatch_get_main_queue(), ^{
// self.displayLabel.text = [NSString stringWithFormat:@"%f",sum];
// NSLog(@"is duo xiancheng = %@",[[NSNumber numberWithBool:[NSThread isMultiThreaded]] stringValue]);
// });
// });
//
dispatch_async(queue, ^{
double sum = 0;
for (int a = i; a < n ; a++)
{
sum = sum + a;
}
NSLog(@"barrier queue 1 = %f",sum);
});
dispatch_async(queue, ^{
double sum = 0;
for (int a = i; a < n ; a++)
{
sum = sum + a;
}
NSLog(@"barrier = %f",sum);
});
dispatch_async(queue, ^{
double sum = 0;
for (int a = i; a < n ; a++)
{
sum = sum + a;
}
NSLog(@"barrier queue 2 = %f",sum);
});
dispatch_async(queue, ^{
double sum = 0;
for (int a = i; a < n ; a++)
{
sum = sum + a;
}
NSLog(@"barrier queue 3 = %f",sum);
});
}
- (void)thread3
{
NSMethodSignature *sig = [self methodSignatureForSelector:@selector(compute:)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
[invocation setTarget:self];
[invocation setSelector:@selector(compute:)];
NSNumber *num = [NSNumber numberWithInt:i];
[invocation setArgument:&num atIndex:2];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithInvocation:invocation];
//[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(compute:) object:];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];
}
- (void)thread4
{
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(compute:) object:[NSNumber numberWithInt:i]];
[thread start];
}
- (void)thread5
{
[self performSelectorInBackground:@selector(compute:) withObject:[NSNumber numberWithInt:i]];
}
0 0
- ios多线程
- iOS多线程
- iOS多线程
- iOS多线程
- 多线程ios
- IOS 多线程
- ios多线程
- ios 多线程
- IOS多线程
- IOS多线程
- iOS多线程
- iOS多线程
- ios 多线程
- iOS多线程
- IOS多线程
- ios多线程
- iOS 多线程
- iOS 多线程
- 【AC自动机】HDU-2222 Keywords Search
- MySQL字符编码的讨论:如何处理emoji等4字节的Unicode字符 - utf8mb4 vs. utf8 Collations
- Python中的推导式介绍
- Android权限大全
- Stanford机器学习---第七讲. 机器学习系统设计
- iOS多线程
- ios学习总结的非常好
- Stanford机器学习---第八讲. 支持向量机SVM
- KJFrameForAndroid框架学习----高效加载Bitmap
- 隐马尔科夫模型(HIDDEN MARKOV MODEL)
- avi 视频
- 获取listview中的子元素view, 设置textview的图标:setCompoundDrawables使用, view.textView.getHeight()为0
- Ubuntu通过xinput禁用及启用联想笔记本的触摸板
- 浏览器标准模式和怪异模式之间的区别是什么?