//
// 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