/**
ios 的线程库模型
1.pthread(底层C)线程库 (一般的情况下不用)
2.NSThread(OC线程库)
3.NSOperationQueue(线程池,线程队列)
4.GCD(Block 模式的线程)
*/
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
SEL sel[3] = {
@selector(nsThread),
@selector(gcd),
@selector(operationQueue)
};
//创建3个子线程
for (NSInteger i = 0; i < 3; i++) {
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:sel[i] object:nil];
[thread start];
}
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark -线程之间的通信(主线程)-
/**
* 主要讲解 主线程和子线程之间的通信,子线程如何获得子线程
*/
-(void)nsThread
{
[self performSelectorOnMainThread:@selector(finished) withObject:self waitUntilDone:YES];
//注意下面这句代码是由子线程来完成的 上面仅仅是获得主线程执行所调用的方法
NSLog(@"nsThread %@",[NSThread currentThread]);
}
-(void)gcd
{
//GCD 获取主线程的方法
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"gcd%@",[NSThread currentThread]);
[self finished];
});
}
-(void)operationQueue
{
//线程池线程队列
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[mainQueue addOperationWithBlock:^{
NSLog(@"mainQueue%@",[NSThread currentThread]);
[self finished];
}];
}
-(void)finished
{
NSLog(@"完成!!!!");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
-(void)NSthread
{
int i =0;
while (i ++ <3) {
[self performSelectorOnMainThread:@selector(finished) withObject:self waitUntilDone:YES];
NSLog(@"%@当前线程NSthread",[NSThread currentThread]);
}
}
-(void)GCD
{
dispatch_async(dispatch_get_main_queue(),^{
NSLog(@"当前线程GCD%@",[NSThread currentThread]);
int i = 0;
while (i++ < 3) {
[NSThread sleepForTimeInterval:1];
[self finished];
}
});
}
-(void)operationQueue2
{
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[mainQueue addOperationWithBlock:^{
int i = 0;
while (i++ < 3) {
[NSThread sleepForTimeInterval:1];
[self finished];
}
NSLog(@"当前线程OPerationQueue2----%@",[NSThread currentThread]);
}];
}
-(void)test
{
//初始化必须给定数组的大小
SEL sel[3] = {
@selector(NSthread),
@selector(GCD),
@selector(operationQueue2)
};
for (NSInteger i = 0; i < 3; i++) {
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:sel[i] object:nil];
[thread start];//用NSThread 创建线程 注意要调用start 方法
}
}
-(IBAction)testBtn:(id)sender{
[self test];
}
@end
2015-08-28 20:47:24.621 线程通信-06练习[3106:202306] 当前线程GCD<NSThread: 0x7fbc22d17880>{number = 1, name = main}
2015-08-28 20:47:25.623 线程通信-06练习[3106:202306] 完成!!!!
2015-08-28 20:47:26.624 线程通信-06练习[3106:202306] 完成!!!!
2015-08-28 20:47:27.625 线程通信-06练习[3106:202306] 完成!!!!
2015-08-28 20:47:27.625 线程通信-06练习[3106:202306] 完成!!!!
2015-08-28 20:47:27.626 线程通信-06练习[3106:202443] <NSThread: 0x7fbc22d91560>{number = 5, name = (null)}当前线程NSthread
2015-08-28 20:47:28.627 线程通信-06练习[3106:202306] 完成!!!!
2015-08-28 20:47:29.628 线程通信-06练习[3106:202306] 完成!!!!
2015-08-28 20:47:30.628 线程通信-06练习[3106:202306] 完成!!!!
2015-08-28 20:47:30.629 线程通信-06练习[3106:202306] 当前线程OPerationQueue2----<NSThread: 0x7fbc22d17880>{number = 1, name = main}
2015-08-28 20:47:30.629 线程通信-06练习[3106:202306] 完成!!!!
2015-08-28 20:47:30.630 线程通信-06练习[3106:202443] <NSThread: 0x7fbc22d91560>{number = 5, name = (null)}当前线程NSthread
2015-08-28 20:47:30.632 线程通信-06练习[3106:202306] 完成!!!!
2015-08-28 20:47:30.634 线程通信-06练习[3106:202443] <NSThread: 0x7fbc22d91560>{number = 5, name = (null)}当前线程NSthread