1.多线程的简介
[self performSelectorInBackground:@selector(logOneToHundred) withObject:nil];
//后台调用某个方法
//打印1-99 和打印@“打印完毕”同时进行 完成之后交给主线程与界面进行交互(谁先完成就先给主线程交互)
[NSThread detachNewThreadSelector:@selector(logOneToHundred) toTarget:self withObject:nil];
方法三:NSThread 线程方法//示例方法
NSThread * thread = [[NSThread alloc]initWithTarget:self selector:@selector(getImageWithUrl) object:nil];
[thread start];
方法四:NSOperationQueue 队列方法(用于多网络请求管理) NSOperation 类似于UIController
第一步:初始化队列
//初始化操作队列--让block方法执行
_operationQueue = [[NSOperationQueue alloc] init];
第二步:用block方法调用方法
NSOperation * blockOperation = [NSBlockOperation blockOperationWithBlock:^{
//这个Block中写入 开辟线程用 来做什么的
[self getImageWithUrl];
}];
[blockOperation setCompletionBlock:^{
//这个block中写入 线程执行完毕后 的操作。。。
[self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:NO];
}];
第三步:将block方法放入声明的队列中
//注意∆:NSOperation的子类实例对象必须放到操作队列中执行
[_operationQueue addOperation:blockOperation];
方法五:NSOperationQueue 队列方法(用于多网络请求管理)NSOperation子类的方法
第一步:声明子类对象,调用方法
NSInvocationOperation * invocationOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(getImageWithUrl) object:nil];
第二步:用block方法调方法
[invocationOperation setCompletionBlock:^{
[self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:NO];
}];
第三步:将block方法放入声明的队列中
[_operationQueue addOperation:invocationOperation];
方法五:自定义NSOperation子类方法
第一步:创建一个继承于NSOperation的自定义类 command + N .h文件中
#import <Foundation/Foundation.h>
@interface CustomOperation : NSOperation
- (void)main;//重写耗时较长的方法
@end
第二步:重写main方法 .m文件中
#import "CustomOperation.h"
@implementation CustomOperation
- (void)main{//重写耗时较长的方法
//当前线程耗时三秒的操作
[NSThread sleepForTimeInterval:3];
}
@end
第三步:声明子类对象,调用方法
CustomOperation * customOperation = [[CustomOperation alloc] init];
第四步:用block方法调方法
[customOperation setCompletionBlock:^{
[self performSelectorOnMainThread:@selector(updateLableWithString:) withObject:@"123"waitUntilDone:NO];
}];
第五步:将block方法放入声明的队列中
[_operationQueue addOperation:customOperation];
方法六:GCD方法
//创建一个并行队列
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//异步执行队列
dispatch_async(globalQueue, ^{
//开辟的线程用来干什么。。。
[self getImageWithUrl];
//执行一个主队列
dispatch_async(dispatch_get_main_queue(), ^{
//刷新界面
[self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:NO];
});
});
NSLock * _lock;//线程锁
_lock = [[NSLock alloc]init];//初始化
第三步:
[NSThread detachNewThreadSelector:@selector(upCount) toTarget:self withObject:nil];
[NSThread detachNewThreadSelector:@selector(downCount) toTarget:self withObject:nil];
- (void)upCount{
while (1) {
// //方法一:
// //保护成员变量每次只能被一个线程访问
// @synchronized(self){
// NSLog(@"%ld", _count +=1);
// }
//方法二:
[_lock lock];//上锁
NSLog(@"%ld", _count +=1);
[_lock unlock];//解锁
}
}
- (void)downCount{
while (1) {
// //保护成员变量每次只能被一个线程访问
// @synchronized(self){
// NSLog(@"%ld", _count -=1);
// }
//方法二:
[_lock lock];//上锁
NSLog(@"%ld", _count -=1);
[_lock unlock];//解锁
}
}