一.创建线程
- (
BOOL
)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
//创建线程的第一种方式
NSThread *
thread
= [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@
"universe"
];
[
thread
start];
[
thread
release];
//创建线程的第二种方式,NSThread类方法
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@
"yuzhou"
];
//创建线程的第三种方法 NSObject方法
[self performSelectorInBackground:@selector(run:) withObject:@
"nsobject thread"
];
//创建线程的第四种方式
NSOperationQueue *oprationQueue = [[NSOperationQueue alloc] init];
[oprationQueue addOperationWithBlock:^{
//这个block语句块在子线程中执行
NSLog(@
"oprationQueue"
);
}];
[oprationQueue release];
//第五种创建线程的方式
NSOperationQueue *oprationQueue1 = [[NSOperationQueue alloc] init];
oprationQueue1.maxConcurrentOperationCount = 1;
//指定池子的并发数
//NSOperation 相当于java中的runnable接口,继承自它的就相当一个任务
NSInvocationOperation *invation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run:) object:@
"invation"
];
[oprationQueue1 addOperation:invation];
//将任务添加到池子里面,可以给池子添加多个任务,并且指定它的并发数
[invation release];
//第二个任务
NSInvocationOperation *invation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run2:) object:@
"invocation2"
];
invation2.queuePriority = NSOperationQueuePriorityHigh;
//设置线程优先级
[oprationQueue1 addOperation:invation2];
[invation2 release];
[oprationQueue1 release];
//调用主线程,用来子线程和主线程交互,最后面的那个boolean参数,如果为yes就是等这个方法执行完了在执行后面的代码;如果为no的话,就是不管这个方法执行完了没有,接着往下走
[self performSelectorOnMainThread:@selector(onMain) withObject:self waitUntilDone:YES];
//---------------------GCD----------------------支持多核,高效率的多线程技术
//创建多线程第六种方式
dispatch_queue_t queue = dispatch_queue_create(
"name"
, NULL);
//创建一个子线程
dispatch_async(queue, ^{
// 子线程code... ..
NSLog(@
"GCD多线程"
);
//回到主线程
dispatch_sync(dispatch_get_main_queue(), ^{
//其实这个也是在子线程中执行的,只是把它放到了主线程的队列中
Boolean isMain = [NSThread isMainThread];
if
(isMain) {
NSLog(@
"GCD主线程"
);
}
});
});
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return
YES;
}
- (
void
)onMain
{
Boolean b = [NSThread isMainThread];
if
(b) {
NSLog(@
"onMain;;%d"
,b);
}
}
- (
void
) run:(NSString*)str
{
NSLog(@
"多线程运行:::%@"
,str);
}
- (
void
) run2:(NSString*)str
{
NSLog(@
"多线程运行:::%@"
,str);
}
二.延时执行
(1)调用NSObject的方法
[self performSelector:@selector(run) withObject:nil afterDelay:2.0];
// 2秒后再调用self的run方法
(2)使用GCD函数
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 2秒后异步执行这里的代码...
});
(3)[NSThread sleepForTimeInterval:10]
2.说明
[self performSelector:@selector(run) withObject:nil afterDelay:3.0];
说明:在3秒钟之后,执行run函数
代码示例:
1 // 2 // YYViewController.m 3 // 01-GCD的常见使用(延迟执行) 4 // 5 // Created by apple on 14-6-25. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 13 @end 14 15 @implementation YYViewController 16 17 - (void)viewDidLoad 18 { 19 [super viewDidLoad]; 20 NSLog(@"打印线程----%@",[NSThread currentThread]); 21 //延迟执行 22 //第一种方法:延迟3秒钟调用run函数 23 [self performSelector:@selector(run) withObject:nil afterDelay:2.0]; 24 25 } 26 -(void)run 27 { 28 NSLog(@"延迟执行----%@",[NSThread currentThread]); 29 } 30 31 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 32 { 33 //在异步函数中执行 34 dispatch_queue_t queue = dispatch_queue_create("wendingding", 0); 35 36 dispatch_sync(queue, ^{ 37 [self performSelector:@selector(test) withObject:nil afterDelay:1.0]; 38 }); 39 NSLog(@"异步函数"); 40 } 41 -(void)test 42 { 43 NSLog(@"异步函数中延迟执行----%@",[NSThread currentThread]); 44 } 45 @end
说明:如果把该方法放在异步函数中执行,则方法不会被调用(BUG?)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//延迟执行的方法
});
说明:在5秒钟之后,执行block中的代码段。
参数说明:
什么时间,执行这个队列中的这个任务。
代码示例:
1 // 2 // YYViewController.m 3 // 02-GCD常见使用(延迟执行2) 4 // 5 // Created by apple on 14-6-25. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 13 @end 14 15 @implementation YYViewController 16 17 - (void)viewDidLoad 18 { 19 [super viewDidLoad]; 20 21 NSLog(@"打印当前线程---%@", [NSThread currentThread]); 22 23 //延迟执行,第二种方式 24 //可以安排其线程(1),主队列 25 dispatch_queue_t queue= dispatch_get_main_queue(); 26 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), queue, ^{ 27 NSLog(@"主队列--延迟执行------%@",[NSThread currentThread]); 28 }); 29 30 //可以安排其线程(2),并发队列 31 //1.获取全局并发队列 32 dispatch_queue_t queue1= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 33 //2.计算任务执行的时间 34 dispatch_time_t when=dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)); 35 //3.会在when这个时间点,执行queue中的这个任务 36 dispatch_after(when, queue1, ^{ 37 NSLog(@"并发队列-延迟执行------%@",[NSThread currentThread]); 38 }); 39 } 40 41 @end
三、一次性代码
1.实现一次性代码
需求:点击控制器只有第一次点击的时候才打印。
实现代码:
1 // 2 // YYViewController.m 3 // 03-GCD常见使用(一次性代码) 4 // 5 // Created by apple on 14-6-25. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 @property(nonatomic,assign) BOOL log; 13 @end 14 15 @implementation YYViewController 16 17 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 18 { 19 if (_log==NO) { 20 NSLog(@"该行代码只执行一次"); 21 _log=YES; 22 } 23 } 24 @end
缺点:这是一个对象方法,如果又创建一个新的控制器,那么打印代码又会执行,因为每个新创建的控制器都有自己的布尔类型,且新创建的默认为NO,因此不能保证改行代码在整个程序中只打印一次。
2.使用dispatch_once一次性代码
使用dispatch_once函数能保证某段代码在程序运行过程中只被执行1次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 只执行1次的代码(这里面默认是线程安全的)
});
整个程序运行过程中,只会执行一次。
代码示例:
1 // 2 // YYViewController.m 3 // 03-GCD常见使用(一次性代码) 4 // 5 // Created by apple on 14-6-25. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 @property(nonatomic,assign) BOOL log; 13 @end 14 15 @implementation YYViewController 16 17 //-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 18 //{ 19 // if (_log==NO) { 20 // NSLog(@"该行代码只执行一次"); 21 // _log=YES; 22 // } 23 //} 24 25 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 26 { 27 static dispatch_once_t onceToken; 28 dispatch_once(&onceToken, ^{ 29 NSLog(@"该行代码只执行一次"); 30 }); 31 } 32 @end