您的位置:首页 > 其它

多线程:线程的安全问题

2016-03-24 14:22 197 查看
//
//  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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: