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多线程开发
概览 大家都知道,在开发过程中应该尽可能减少用户等待时间,让程序尽可能快的完成运算.可是无论是哪种语言开发的程序最终往往转换成汇编语言进而解释成机器码来执行.但是机器码是按顺序执行的,一个复杂的多步操 ...
随机推荐
- 网络爬虫urllib2 tornado
百度不支持用tornado请求,可以用美团开放API 测试. import tornado.httpclient def fetch(url): http_header={'User-Agent':' ...
- js中加密及设置cookie
1.设置cookie及有效期时长 //cname:cookie的名称,cvalue:cookie的内容,exdays:cookie有效期时长: function setCookie(cname, cv ...
- [转载]我们可以用SharePoint做什么
前言 不知不觉作为一个SharePoint的开发人员若干年了,从SharePoint API开始学习,到了解SharePoint的结构,逐渐一点点了解sharepoint的体系:从SharePoint ...
- Robot Framework自动化测试(一)---第一个脚本
最近工具中用Robot Framework框架来做自动化,所以,花时间学习了一下. =======所需环境=================== Python: https://www.python. ...
- Python单元测试框架之pytest -- fixtures
fixtures不太好翻译,可看作是夹心饼干最外层的两片饼干.通常用setup/teardown来表示.它主要用来包裹测试用例,为什么需要这样的饼干呢?我们以web自动化测试为例,例如,要测试的某系统 ...
- python编码声明的位置很重要
python在3.x版本之前,编码一直是一个很头痛的问题.在代码中如果要使用中文,通常都要在文件的头部注明# -*- coding:utf-8 -*- 这样IDE或者解释器才会智能的转换编码. 这其中 ...
- An Introduction to Garbage Collection(垃圾回收简介)
1. Introduction 2. Principles 3. Advantages 4. Disadvantages 5. 常见的垃圾回收技术 5.1. 跟踪式垃圾回收 5.1.1. 基本算法 5 ...
- MySQL中自己不太常用的命令
一 更改表的信息 alter table students add primary key (stunum); 为已经创建好的表添加主键. alter语句不仅可以添加主键,还可以对已经创建好的表修改一 ...
- Node.js下基于Express + Socket.io 搭建一个基本的在线聊天室
一.聊天室简单介绍 采用nodeJS设计,基于express框架,使用WebSocket编程之 socket.io机制.聊天室增加了 注册登录模块 ,并将用户个人信息和聊天记录存入数据库. 数据库采用 ...
- 一个端口操作类PortHelper
此操作类主要使用了系统命令行来实现的,详细的命令可参照微软文档<如何使用"netsh advfirewall firewall"上下文而非“netsh firewall”上下 ...