iOS多线程
来源:互联网 发布:17%退税率出口退税算法 编辑:程序博客网 时间:2024/06/06 10:42
多线程的概念不再赘述,iOS开发中总的来讲可用的多线程方法一共有四种。
pthread
NSThread
面向对象,偶尔使用,需要手动管理生命周期,其实也只是需要你手动去开启线程。
// 简单用法
// 创建线程
NSThread *thread = [[NSThreadalloc] initWithTarget:selfselector:@selector(run:)object:@"jack"];
thread.name =@"my-thread";
// 启动线程
[thread start];
[NSThreaddetachNewThreadSelector:@selector(run:)toTarget:selfwithObject:@"rose"];
// 消息传递
[selfperformSelectorInBackground:@selector(run:)withObject:@"jack"];
还有一些其他常用方法,具体不再赘述。
GCD
基于c语言,能够充分利用设备的多核,具体方法如下
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//GCD:主要关注两个内容
//同步执行, 异步执行;
//队列: 主对列,全局子队列, 自定义队列;
//主队列:
dispatch_queue_t mainQueue =dispatch_get_main_queue(); // 获取主队列;
//获取全局子队列:
//优先级:
// DISPATCH_QUEUE_PRIORITY_DEFAULT 默认优先级:
dispatch_queue_t globalQueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
#if 0
//在主对列发起同步任务, 主线程等待等待任务执行,任务又在主线程里面等待主线程,造成互相等待, 卡顿:
//在主队列发起同步任务:
dispatch_sync(mainQueue, ^{
NSLog(@"%@", [NSThread currentThread]);
});
#endif
#if 0
//在全局队列发起同步任务:
dispatch_sync(globalQueue, ^{
NSLog(@"%@", [NSThread currentThread]);
});
#endif
#if 0
//用于在子线程回到主线程执行: (常用做法) √
dispatch_async(mainQueue, ^{
NSLog(@"%@", [NSThread currentThread]);
});
#endif
#if 0
//异步任务, 全局子队列: (用到) √
//会开启子线程执行任务:
//可以开启多条子线程:
dispatch_async(globalQueue, ^{
NSLog(@"%@", [NSThread currentThread]);
});
dispatch_async(globalQueue, ^{
NSLog(@"%@", [NSThread currentThread]);
});
#endif
#if 0
//参数二: 0 表示串行队列:
dispatch_queue_t customQueue1 = dispatch_queue_create("custom",0);
//串行队列只创建一条子线程,所有任务在这条线程里串行执行:
dispatch_async(customQueue1, ^{
NSLog(@"%@", [NSThread currentThread]);
});
dispatch_async(customQueue1, ^{
NSLog(@"%@", [NSThread currentThread]);
});
#endif
//参数二: DISPATCH_QUEUE_CONCURRENT 表示并发队列:
dispatch_queue_t customQueue2 =dispatch_queue_create("custom2",DISPATCH_QUEUE_CONCURRENT);
//并发队列,可以开启多条线程:
dispatch_async(customQueue2, ^{
NSLog(@"%@", [NSThreadcurrentThread]);
});
dispatch_async(customQueue2, ^{
NSLog(@"%@", [NSThreadcurrentThread]);
});
//一段时间执行对应的代码:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
UIAlertView *a = [[UIAlertViewalloc] initWithTitle:@"弹框"message:nildelegate:nilcancelButtonTitle:nilotherButtonTitles:@"确定",nil];
[a show];
});
NSLog(@".....");
//只会执行一次:
// static dispatch_once_t onceToken;
// dispatch_once(&onceToken, ^{
// <#code to be executed once#>
// });
// [SingleExample shareSingle];
// [[SingleExample alloc] init];
// [[SingleExample alloc] init];
// [UIApplication sharedApplication];
// [NSNotificationCenter defaultCenter];
// [NSFileManager defaultManager];
NSLog(@"%p", [SingleExampleshareSingle]);
NSLog(@"%p", [SingleExampleshareSingle]);
NSLog(@"%p", [SingleExampleshareSingle]);
// NSLog(@"%p", [[SingleExample alloc] init]);
//分组执行任务:
//创建分组:
dispatch_group_t group =dispatch_group_create();
//异步执行第一组任务:
dispatch_group_async(group, globalQueue, ^{
NSLog(@"第一个任务%@", [NSThreadcurrentThread]);
});
dispatch_group_async(group, globalQueue, ^{
NSLog(@"第二个任务%@", [NSThreadcurrentThread]);
});
//等待其他任务执行完才执行:
dispatch_group_notify(group, globalQueue, ^{
NSLog(@"第三个任务%@", [NSThreadcurrentThread]);
});
[NSThreaddetachNewThreadSelector:@selector(doSomeThing:)toTarget:selfwithObject:nil];
}
- (void)doSomeThing:(id)obj
{
//下载网络数据:
//返回主线程刷新UI界面:
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"refreshUI%@", [NSThreadcurrentThread]);
});
}
单粒写法
static SingleExample *single =nil;
@implementation SingleExample
+ (SingleExample *)shareSingle
{
staticdispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
single = [[SingleExamplealloc] init];
});
returnsingle;
}
NSOperation
@interface ViewController ()
// 线程队列,也称为线程池;
@property (nonatomic,strong) NSOperationQueue *queue;
@end
@implementation ViewController
- (NSOperationQueue *)queue
{
if (_queue ==nil) {
_queue = [[NSOperationQueuealloc] init];
}
return_queue;
}
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)doOp1:(id)obj
{
for (int i =0; i < 5; i++) {
NSLog(@"op1");
[NSThreadsleepForTimeInterval:1.0];
}
}
- (IBAction)clickStart:(id)sender {
// NSOperation 的子类
// 通过方法创建任务:
// 执行方法里面的代码:
NSInvocationOperation *op1 = [[NSInvocationOperationalloc] initWithTarget:selfselector:@selector(doOp1:)object:nil];
// 通过block方法创建任务:
// 执行block里面的代码:
NSBlockOperation *op2 = [NSBlockOperationblockOperationWithBlock:^{
NSLog(@"op2");
}];
NSBlockOperation *op3 = [NSBlockOperationblockOperationWithBlock:^{
NSLog(@"op3");
}];
// 最大的线程个数:
// self.queue.maxConcurrentOperationCount = 1; //串行执行;
//依赖顺序关系:
[op2 addDependency:op1]; // op2等待op1执行完才执行;
[op3 addDependency:op1]; // op3等待op1执行完才执行;
// op2,op3 与 op1 串行;
// op2和op3并发执行
// 把任务放到线程队列里:
[self.queueaddOperation:op3];
[self.queueaddOperation:op2];
[self.queueaddOperation:op1];
// [op1 cancel];
}
- (IBAction)clickStop:(id)sender {
// 取消正在等待的任务:
[self.queuecancelAllOperations];
}
@end
NSLock *lock = [[NSLockalloc] init];
//加锁
[lock lock];
// 中间是访问的方法
// method
//解锁
[lock unlock];
第二种就是关键字synchronized
示例方法
while (1) {
@synchronized(self) {
// method
}
}
- ios多线程
- iOS多线程
- iOS多线程
- iOS多线程
- 多线程ios
- IOS 多线程
- ios多线程
- ios 多线程
- IOS多线程
- IOS多线程
- iOS多线程
- iOS多线程
- ios 多线程
- iOS多线程
- IOS多线程
- ios多线程
- iOS 多线程
- iOS 多线程
- Codeforces 672E:Ultimate Weirdness of an Array 线段树
- LeetCode 145. Binary Tree Postorder Traversal
- DP 动态规划 Problem E 1005 最大递增子序列
- Not Fibonacci(矩阵连乘)
- ZOJ 3600 Taxi Fare (数学)
- iOS多线程
- LaTeX技巧001:ctex下使用其他中文字体
- 使用Jmeter进行http接口测试
- QT信号槽多次连接引起的错误修改
- PHP第三方登录—QQ登录
- centos 的权限管理--自主访问控制DAC和访问控制列表 ACL
- listener.ora
- hdu_4824_Disk Schedule(dp)
- hdoj 5681 zxa and wifi