iOS多线程初见
. 三种创建线程的方法
//第一种
NSThread * thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(doAction) object:nil];
//线程名
thread1.name = @"thread1";
//线程优先级,0 ~ 1
thread1.threadPriority = 1.0;
//开启线程
[thread1 start];
//第二种
//通过类方法创建线程,不用显示的开启start
[NSThread detachNewThreadSelector:@selector(doAction) toTarget:self withObject:nil];
//第三种
//隐式创建多线程
[self performSelectorInBackground:@selector(doAction:) withObject:nil];
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"mainThread - %@",[NSThread mainThread]);
NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(handleAction) object:nil];
//就绪状态
[thread start];
}
- (void)handleAction {
for (NSInteger i = ; i < ; i ++) {
//阻塞状态
// [NSThread sleepForTimeInterval:2];
// [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
// NSLog(@"%@,%@",[NSThread currentThread],@(i));
//可以在子线程获取主线程
NSLog(@"mainThread - %@",[NSThread mainThread]);
if (i == ) {
//退出
[NSThread exit];
}
}
}
.同步代码块实现买票功能
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tickets = ;
NSThread * thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
// thread1.name = @"computer";
[thread1 start];
NSThread * thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
// thread2.name = @"phone";
[thread2 start];
}
- (void)saleTicket {
while () {
// [NSThread sleepForTimeInterval:1];
//token必须所有线程都能访问到,一般用self
// @synchronized() {
//代码段
// }
// NSObject * o = [[NSObject alloc] init];
//互斥锁
@synchronized(self) {
[NSThread sleepForTimeInterval:];
if (self.tickets > ) {
NSLog(@"%@ 还有余票 %@ 张",[NSThread currentThread],@(self.tickets));
self.tickets -- ;
} else {
NSLog(@"票卖完了");
break;
}
}
}
}
iOS多线程初见的更多相关文章
- iOS多线程主题
下面是:2个并发进程.和2个并发线程的示意图: 下面介绍三种多线程技术(Thread.Cocoa Operation.Grand Central Dispatch): 1.最轻量级Thread(需要自 ...
- iOS多线程技术方案
iOS多线程技术方案 目录 一.多线程简介 1.多线程的由来 2.耗时操作的模拟试验 3.进程和线程 4.多线程的概念及原理 5.多线程的优缺点和一个Tip 6.主线程 7.技术方案 二.Pthrea ...
- iOS 多线程GCD的基本使用
<iOS多线程简介>中提到:GCD中有2个核心概念:1.任务(执行什么操作)2.队列(用来存放任务) 那么多线程GCD的基本使用有哪些呢? 可以分以下多种情况: 1.异步函数 + 并发队列 ...
- iOS多线程到底不安全在哪里?
iOS多线程安全的概念在很多地方都会遇到,为什么不安全,不安全又该怎么去定义,其实是个值得深究的话题. 共享状态,多线程共同访问某个对象的property,在iOS编程里是很普遍的使用场景,我们就从P ...
- iOS多线程的详情使用示例--简进祥
大家都知道,在开发过程中应该尽可能减少用户等待时间,让程序尽可能快的完成运算.可是无论是哪种语言开发的程序最终往往转换成汇编语言进而解释成机器码来执行.但是机器码是按顺序执行的,一个复杂的多步操作只能 ...
- iOS多线程
关于iOS多线程 概述 这篇文章中,我不会说多线程是什么.线程和进程的区别.多线程有什么用,当然我也不会说什么是串行.什么是并行等问题,这些我们应该都知道的. 在 iOS 中其实目前有 4 套多线程方 ...
- iOS多线程学习及总结
能有份网上的存储资料,备以后提升及参考 iOS 多线程编程 简介 一. iOS有三种多线程编程的技术,分别是: 1. NSThread 2. Cocoa NSOp ...
- iOS多线程杂论
iOS多线程的分布 (1) NSThread (2) NSOperation (3) GCD 现在对下面三个进行一个个的分析,希望那里说得不对的地方希望简友们帮我指点一二. 1,NSThread 优点 ...
- iOS多线程开发
概览 大家都知道,在开发过程中应该尽可能减少用户等待时间,让程序尽可能快的完成运算.可是无论是哪种语言开发的程序最终往往转换成汇编语言进而解释成机器码来执行.但是机器码是按顺序执行的,一个复杂的多步操 ...
随机推荐
- glassfish服务器默认的网页所在的位置
http://localhost:8080/ 默认打开的网页所在的位置 E:/glassfish-4.1/glassfish/domains/domain1/docroot/index.html
- 【转】github上值得关注的前端项目
综合/资源 frontend-dev-bookmarks 一个巨大的前端开发资源清单.star:15000 front-end-collect 分享自己长期关注的前端开发相关的优秀网站.博客.以及活跃 ...
- rabbitmq使用dead letter机制来进行retry
首先建立 工作exchange和工作queue,指定工作队列的x-dead-letter-exchange到重试exchenge var workQueueArgs = new Dictionary& ...
- bootstrap插件学习-bootstrap.alert.js
我们先看bootstrap.alert.js的结构 var dismiss = '[data-dismiss="alert"]' //自定义属性 Alert = function ...
- Hadoop第5周练习—MapReduce计算气象温度等例子
:对云计算的看法 内容 :使用MapReduce求每年最低温度 内容 :求温度平均值能使用combiner吗? 内容 :使用Hadoop流求最高温度(awk脚本) 内容 :使用Hadoop流求最高温度 ...
- LeetCode-334. Increasing Triplet Subsequence
Description: Given an unsorted array return whether an increasing subsequence of length 3 exists or ...
- SQL Server临界点游戏——为什么非聚集索引被忽略!
当我们进行SQL Server问题处理的时候,有时候会发现一个很有意思的现象:SQL Server完全忽略现有定义好的非聚集索引,直接使用表扫描来获取数据.我们来看看下面的表和索引定义: CREATE ...
- ActiveMQ学习(二)——MQ的工作原理
如图所示 首先来看本地通讯的情况,应用程序A和应用程序B运行于同一系统A,它们之间可以借助消息队列技术进行彼此的通讯:应用程序A向队列1发送一条信息,而当应用程序B需要时就可以得到该信息. 其次是远程 ...
- VS问题汇总——竹子整理
VS这个宇宙第一IDE有时候也会淘气一把,此贴记录下日后遇到的VS本身的问题 1.VS显示正忙!!!其他项目能打开,就这个不行,关闭vs进程重启vs还是无法解决. 这个问题从VS2010开始就遇到过, ...
- 奥特曼小分队之四(Work Breakdown Structure)
写在前面的话:游戏介绍http://www.cnblogs.com/atmxfd/p/5415107.html 需求 我们的游戏是一款基于局域网的游戏,用户只需将服务端和客户端置于同一局域网下即可使用 ...