多线程创建 方法一: NSThread 创建线程的三种方式
2014-11-26 19:54
351 查看
// // RootViewController.m // 多线程 // // Created by zm on 14-11-26. // Copyright (c) 2014年 practice. All rights reserved. // #import "RootViewController.h" @interface RootViewController () { UILabel *_textLabel; // 互斥锁 NSLock *_lock ; // 条件锁 NSConditionLock *_conditionLock; } @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { [self prepareView]; } return self; } -(void)prepareView { _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 30)]; _textLabel.text = @"test"; [self.view addSubview:_textLabel]; } - (void)viewDidLoad { [super viewDidLoad]; /** * 主线程 有序执行 * 主线程就是 UI 线程 * 官方文档:主线程主要处理: * 1. UI 界面,刷新 * 2. 用户触摸事件 */ [self doSomething:@"D"]; [self doSomething:@"E"]; [self doSomething:@"F"]; /** * 理论上(根据官方文档)子线程不能刷新 UI 界面 * 子线程主要处理: * 1.耗时操作 * 2.数据库等操作 * * 实现多线程的方法: 三种 * 1. NSThread 软件模拟实现 * 2.NSOperationQueue 线程池 * 3.GCD 苹果大力推荐的方式!(重点) */ /** * 创建的线程 无序执行 * 线程与主线程之间无序,一个线程可以发生在任意主线程前面执行 * 线程与线程之间也无序,一个线程可以发生在任意子线程前面执行 */ //[self testNSThreadSimplely]; //[self testNSLock]; [self testConditionLock]; } // NSThread 三种创建方式,简单介绍 -(void)testNSThreadSimplely { /** * 创建 NSThread 三种方式 * 创建的线程 无序执行 */ /** * 1. 创建一个线程,并且运行 * [self performSelectorInBackground:@selector() withObject:@""]; */ [self performSelectorInBackground:@selector(doSomething:) withObject:@"A"]; [self performSelectorInBackground:@selector(doSomething:) withObject:@"B"]; [self performSelectorInBackground:@selector(doSomething:) withObject:@"C"]; /** * 2.创建 NSThread 第二种方式 * [NSThread detachNewThreadSelector:@selector() toTarget:self withObject:@""]; */ [NSThread detachNewThreadSelector:@selector(doSomething:) toTarget:self withObject:@"G"]; /** * 3.创建 NSThread 第三种方式 * 主动创建,调用(启动)线程 */ NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(doSomething:) object:@"H"]; [thread start]; } -(void)doSomething:(NSString *)str { //当前线程休眠一秒钟 [NSThread sleepForTimeInterval:1.0]; NSLog(@"str = %@",str); _textLabel.text = str; } /** * 测试 互斥锁 : 买票 */ -(void)testNSLock { _lock = [[NSLock alloc] init]; [self performSelectorInBackground:@selector(getTicket) withObject:nil]; [self performSelectorInBackground:@selector(getTicket) withObject:nil]; [self performSelectorInBackground:@selector(getTicket) withObject:nil]; [self performSelectorInBackground:@selector(getTicket) withObject:nil]; } //买票操作 int sum = 10; int sold = 0; -(void)getTicket { [_lock lock]; //加锁 while (sum > 0) { sum--; sold++; NSLog(@"sum = %d; sold = %d",sum,sold); } [_lock unlock]; //解锁 } /** * 测试条件锁 */ -(void)testConditionLock { _conditionLock = [[NSConditionLock alloc] init]; [self performSelectorInBackground:@selector(conditionLockDoSomethingA) withObject:nil]; [self performSelectorInBackground:@selector(conditionLockDoSomethingB) withObject:nil]; } -(void)conditionLockDoSomethingA { [_conditionLock lock]; for (int i = 0; i < 10; i++) { NSLog(@"i = %d",i); [NSThread sleepForTimeInterval:1]; if (i==5) [_conditionLock unlockWithCondition:5]; } } -(void)conditionLockDoSomethingB { [_conditionLock lockWhenCondition:5]; NSLog(@"condition = 5"); [_conditionLock unlock]; } @end
相关文章推荐
- SE高阶(4):多线程(并发)—①创建启动方式和控制线程方法
- Java学习之多线程--创建线程的三种方式
- 多线程编程——创建线程的三种方法
- 0036 Java学习笔记-多线程-创建线程的三种方式
- Java基础:多线程(1)--线程的概述、创建线程的方式、线程的多种状态、线程常用的方法
- 多线程-创建线程的三种方式
- NSThread创建线程的三种方式
- 1多线程的概述2多线程(创建多个线程实例,并启动多个线程)的实现方式,main主方法是单线程的4多线程的实现方式5多线程模拟火车站售票出现问题7线程的声明周期
- 通过GCD、NSOperationQueue队列、NSThread三种方法来创建多线程
- 一心多用多线程-线程创建的三种方式
- java 多线程(一)---创建线程的三种方式Thread,Runnable,Callable与Future
- Java多线程之创建线程的三种方式比较
- 【多线程】创建线程的三种方式
- 05-多线程(多线程创建的方式一-继承Thread类)1 2 06-多线程(Thread类中的方法&线程名称) 08-多线程(线程的状态)图解
- 多线程基础知识第一篇:创建线程的方式及线程常用的方法
- 初学Java多线程:向线程传递数据的三种方法
- java多线程总结一:线程的两种创建方式及优劣比较
- SQLServer 中存储过程返回的三种方式( 包括存储过程的创建, 在存储过程中调用, 在VS中调用的方法)
- java多线程总结一:线程的两种创建方式及优劣比较
- java创建线程的三种方式