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多线程开发
概览 大家都知道,在开发过程中应该尽可能减少用户等待时间,让程序尽可能快的完成运算.可是无论是哪种语言开发的程序最终往往转换成汇编语言进而解释成机器码来执行.但是机器码是按顺序执行的,一个复杂的多步操 ...
随机推荐
- sqlserver -- 学习笔记(四)将一个数据库的表复制到另外一个数据库(备忘)
--复制结构+数据 select * into 数据库名.dbo.新表名 from 数据库名.dbo.原表名 select * into Stockholder.dbo.SHInfo from dsp ...
- 【转载】Grunt常用插件介绍
项目名称 grunt-contrib v0.8.0 项目地址 https://github.com/gruntjs/grunt-contrib 项目介绍 此项目是对grunt常用插件的集合,刚接触gr ...
- 想从事分布式系统,计算,hadoop等方面,需要哪些基础,推荐哪些书籍?--转自知乎
作者:廖君链接:https://www.zhihu.com/question/19868791/answer/88873783来源:知乎 分布式系统(Distributed System)资料 < ...
- asynchronous-logging-with-log4j-2--转
原文地址:https://dzone.com/articles/asynchronous-logging-with-log4j-2 Log4J 2 is a logging framework des ...
- HMM 自学教程(四)隐马尔科夫模型
本系列文章摘自 52nlp(我爱自然语言处理: http://www.52nlp.cn/),原文链接在 HMM 学习最佳范例,这是针对 国外网站上一个 HMM 教程 的翻译,作者功底很深,翻译得很精彩 ...
- Direct3D11学习:(三)Direct3D11初始化
转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 做完一系列的准备工作之后,我们就正式进入Direct3D11的学习了.我们就从Direct3D11的初始化工作开 ...
- Android 学习笔记之AndBase框架学习(一) 实现多功能标题栏
PS:Volley框架终于通过看源码的方式完成了所有的学习..开始学习AndBase...AndBase的源码实在是多的离谱...因此就不对所有的源码进行分析了... 学习内容: 1.使用AndBas ...
- Hekaton是如何影响你数据库的目标恢复时间(RTO)的
这个周末我发现了SQL Server 2014里Hekaton的一个有趣副作用,很遗憾它会负面影响你数据库的目标恢复时间(Recovery Time Objective,RTO).你已知道,对于每个本 ...
- MyBatis魔法堂:各数据库的批量Update操作
一.前言 MyBatis的update元素的用法与insert元素基本相同,因此本篇不打算重复了.本篇仅记录批量update操作的sql语句,懂得SQL语句,那么MyBatis部分的操作就简单了. ...
- ADO.NET常用对象的基础概念强化
1.Command对象 1.1 ExcuteNonquery---执行非查询语句,返回受影响的行数,在新增,删除,修改的时候,如果我们要返回结果集那么就不能使用它了: 1.2 ExcuteScalar ...