多线程创建 方法一: NSThread 创建线程的三种方式



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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值