多线程:线程的安全问题

//
//  ViewController.m
//  06-线程安全
//
//  Created by gzxzmac on 16/1/28.
//  Copyright © 2016年 gzxzmac. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, assign) NSInteger ticket;// 总共的票数
@property (nonatomic, strong) NSObject *obj;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.obj = [[NSObject alloc]init];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.ticket = 20;

    // 创建线程卖票
    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil];
    // 设置线程的名字
    thread.name = @"售员01";
    // 开始卖票
    [thread start];
    //
    // 创建线程卖票
    NSThread *thread1 = [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil];
    // 设置线程的名字
    thread1.name = @"售员02";
    // 开始卖票
    [thread1 start];
}

/*
 1. 要做到卖出去的票是正确的,而且刚好卖完

 2. 使用互斥锁可以解决线程共享资源问题
 3. 互斥锁的作用:锁内的代码,在同一时间,只能在一个线程上执行
 4. 互斥锁使用的资源会比较多,锁内的代码越少越好,锁住线程共享属性(变量)的setter 和 getter
 */
- (void)saleTicket {

    // 互斥锁
    while (YES) {
        // 设置休眠
        [NSThread sleepForTimeInterval:1.f];
        // 锁对象 -> 所有的对象都可以.不要使用局部变量.一般情况下使用self
//        NSObject *obj = [[NSObject alloc]init];
        @synchronized(self) {
            // 需要上锁的代码
            // 判断是否有票
            if (self.ticket > 0) {
                self.ticket --;
                NSLog(@"卖出一张票 %zd %@",self.ticket,[NSThread currentThread]);

                // 下一个
                continue;
            }
            // 卖完之后才有可能执行
            NSLog(@"卖完了");
            break;
//            else {// 卖完了
//                NSLog(@"卖完了");
//                break;
//            }
        }
    }
}



@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值