多线程同步之@synchronized实战例子

场景:售票点有100W张门票,10个售票站,计算机开10个线程一起在卖票,每个线程卖10W张门票,0.1毫秒卖出一张票,卖完为止。
我们预期的结果是:当10个线程都卖完票的时候,总票数为0。
简单一点,直接上代码,门票类Ticket.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN
/**
门票
*/
@interface Ticket : NSObject

@property (nonatomic, assign) int ticketNum; // 票数

@end

NS_ASSUME_NONNULL_END

Ticket.m

#import "Ticket.h"

@implementation Ticket

@end

ViewController.m开10个线程一起卖票,10个线程都卖完的时候,点击视图,查看总票数。

#import "ViewController.h"
#import "Ticket.h"           // 门票

@interface ViewController ()

@property (nonatomic, strong) Ticket *ticket;  // 门票

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.ticket = [[Ticket alloc] init];
    _ticket.ticketNum = 1000000;   // 100W张门票
    [self testSellTickets];        // // 开10个线程卖票
}

// 线程函数
- (void)threadFunc:(id)obj {
    // 每一个线程卖掉10W张门票
    for (int i = 0; i < 100000; i++) {
        _ticket.ticketNum = _ticket.ticketNum - 1;
        [NSThread sleepForTimeInterval:0.0001];
    }
    
    NSLog(@"thread[%@] 票卖完了!!!", obj);
}

// 多线程卖票
- (void)testSellTickets {
    // 创建10个线程
    for (int i = 1; i <= 10; i++) {
        NSNumber *number = [NSNumber numberWithInt:i]; // 线程编号
        [NSThread detachNewThreadSelector:@selector(threadFunc:) toTarget:self withObject:number];
    }
}

// 点击视图
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"剩余总票数 = %d", _ticket.ticketNum);
}

@end

打印输出:

2020-04-05 12:04:55.458628+0800 MultThreadSyncDemo[1934:35174] thread[2] 票卖完了!!!
2020-04-05 12:04:55.458960+0800 MultThreadSyncDemo[1934:35179] thread[7] 票卖完了!!!
2020-04-05 12:04:55.459427+0800 MultThreadSyncDemo[1934:35177] thread[5] 票卖完了!!!
2020-04-05 12:04:55.459823+0800 MultThreadSyncDemo[1934:35180] thread[8] 票卖完了!!!
2020-04-05 12:04:55.460188+0800 MultThreadSyncDemo[1934:35176] thread[4] 票卖完了!!!
2020-04-05 12:04:55.461332+0800 MultThreadSyncDemo[1934:35182] thread[10] 票卖完了!!!
2020-04-05 12:04:55.461882+0800 MultThreadSyncDemo[1934:35181] thread[9] 票卖完了!!!
2020-04-05 12:04:55.462926+0800 MultThreadSyncDemo[1934:35178] thread[6] 票卖完了!!!
2020-04-05 12:04:55.463071+0800 MultThreadSyncDemo[1934:35175] thread[3] 票卖完了!!!
2020-04-05 12:04:55.464582+0800 MultThreadSyncDemo[1934:35173] thread[1] 票卖完了!!!
2020-04-05 12:05:02.978691+0800 MultThreadSyncDemo[1934:35092] 剩余总票数 = 26103

怎么回事,还有26103张票没有卖完?
这就是多线程同步的问题,每个线程都在操作_ticket.ticketNum这个属性,
语句:_ticket.ticketNum = _ticket.ticketNum - 1;
比如线程1拿到_ticket.ticketNum的值,在做 - 1的这一刻,线程2已经给_ticket.ticketNum赋上新的值了。也就是说线程1_ticket.ticketNum拿到了旧值。线程1应该拿到新的值去减才是正确。
那么,如何保证线程1在操作_ticket.ticketNum的时候,其他线程不去操作_ticket.ticketNum?
咱们引入多线程同步@synchronized来解决问题

咱们修改一下ViewController.m的线程函数threadFunc

// 线程函数
- (void)threadFunc:(id)obj {
    // 每一个线程卖掉10W张门票
    for (int i = 0; i < 100000; i++) {
        @synchronized (_ticket) {
            _ticket.ticketNum = _ticket.ticketNum - 1;
        }
        [NSThread sleepForTimeInterval:0.0001];
    }
    
    NSLog(@"thread[%@] 票卖完了!!!", obj);
}

再次运行,打印输出:

2020-04-05 12:12:56.380468+0800 MultThreadSyncDemo[2063:38818] thread[9] 票卖完了!!!
2020-04-05 12:12:56.380965+0800 MultThreadSyncDemo[2063:38815] thread[6] 票卖完了!!!
2020-04-05 12:12:56.381700+0800 MultThreadSyncDemo[2063:38817] thread[8] 票卖完了!!!
2020-04-05 12:12:56.381759+0800 MultThreadSyncDemo[2063:38814] thread[5] 票卖完了!!!
2020-04-05 12:12:56.382018+0800 MultThreadSyncDemo[2063:38812] thread[3] 票卖完了!!!
2020-04-05 12:12:56.382316+0800 MultThreadSyncDemo[2063:38819] thread[10] 票卖完了!!!
2020-04-05 12:12:56.383213+0800 MultThreadSyncDemo[2063:38813] thread[4] 票卖完了!!!
2020-04-05 12:12:56.383451+0800 MultThreadSyncDemo[2063:38816] thread[7] 票卖完了!!!
2020-04-05 12:12:56.384270+0800 MultThreadSyncDemo[2063:38810] thread[1] 票卖完了!!!
2020-04-05 12:12:56.388746+0800 MultThreadSyncDemo[2063:38811] thread[2] 票卖完了!!!
2020-04-05 12:12:58.781731+0800 MultThreadSyncDemo[2063:38732] 剩余总票数 = 0

OK,剩余总票数 = 0。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值