美文网首页
多线程售卖火车票(非线程安全)

多线程售卖火车票(非线程安全)

作者: Jean_Lina | 来源:发表于2020-07-06 16:59 被阅读0次
@property(nonatomic, assign)NSInteger ticketSurplusCount;

#pragma mark 初始化火车票数量、卖票窗口(非线程安全)、并开始卖票
- (void)initTicketStatusNotSave {
    //打印当前线程
    NSLog(@"currentThread---%@",[NSThread currentThread]);
    self.ticketSurplusCount = 50;
    // queue1 代表北京火车票售卖窗口
    dispatch_queue_t queue1 = dispatch_queue_create("net.bujige.testQueue1", DISPATCH_QUEUE_SERIAL);
    // queue2 代表上海火车票售卖窗口
    dispatch_queue_t queue2 = dispatch_queue_create("net.bujige.testQueue2", DISPATCH_QUEUE_SERIAL);
    // queue3 代表深圳火车票售卖窗口
    dispatch_queue_t queue3 = dispatch_queue_create("net.bujige.testQueue3", DISPATCH_QUEUE_SERIAL);
    // queue4 代表天津火车票售卖窗口
    dispatch_queue_t queue4 = dispatch_queue_create("net.bujige.testQueue4", DISPATCH_QUEUE_SERIAL);
    // queue5 代表广州火车票售卖窗口
    dispatch_queue_t queue5 = dispatch_queue_create("net.bujige.testQueue5", DISPATCH_QUEUE_SERIAL);
    NSLog(@"火车票售票---begin");
    __weak typeof(self) weakSelf = self;
    dispatch_async(queue1, ^{
        NSLog(@"北京");
        [weakSelf saleTicketNotSafe];
    });
    dispatch_async(queue2, ^{
        NSLog(@"上海");
        [weakSelf saleTicketNotSafe];
    });
    dispatch_async(queue3, ^{
        NSLog(@"深圳");
        [weakSelf saleTicketNotSafe];
    });
    dispatch_async(queue4, ^{
        NSLog(@"天津");
        [weakSelf saleTicketNotSafe];
    });
    dispatch_async(queue5, ^{
        NSLog(@"广州");
        [weakSelf saleTicketNotSafe];
    });
}
#pragma mark 售卖火车票(非线程安全)
- (void)saleTicketNotSafe {
    while (1) {
        if (self.ticketSurplusCount > 0) {
            //如果还有票,继续售卖
            self.ticketSurplusCount--;
            NSLog(@"%@", [NSString stringWithFormat:@"剩余票数:%ld 窗口:%@", self.ticketSurplusCount, [NSThread currentThread]]);
            [NSThread sleepForTimeInterval:0.2];
        } else {
            //如果已卖完,关闭售票窗口
            NSLog(@"所有火车票均已售完");
            break;
        }
    }
}

输出结果:
2020-07-06 16:50:58.947150+0800 GCD[3445:1211597] 剩余票数:6 窗口:<NSThread: 0x28399af00>{number = 7, name = (null)}
2020-07-06 16:50:58.947299+0800 GCD[3445:1211596] 剩余票数:5 窗口:<NSThread: 0x28399c140>{number = 4, name = (null)}
2020-07-06 16:50:59.141898+0800 GCD[3445:1211591] 剩余票数:3 窗口:<NSThread: 0x28399c100>{number = 3, name = (null)}
2020-07-06 16:50:59.141835+0800 GCD[3445:1211593] 剩余票数:4 窗口:<NSThread: 0x2839876c0>{number = 6, name = (null)}
2020-07-06 16:50:59.151231+0800 GCD[3445:1211592] 剩余票数:2 窗口:<NSThread: 0x283987c00>{number = 5, name = (null)}
2020-07-06 16:50:59.151256+0800 GCD[3445:1211597] 剩余票数:1 窗口:<NSThread: 0x28399af00>{number = 7, name = (null)}
2020-07-06 16:50:59.151362+0800 GCD[3445:1211596] 剩余票数:0 窗口:<NSThread: 0x28399c140>{number = 4, name = (null)}
2020-07-06 16:50:59.345881+0800 GCD[3445:1211591] 所有火车票均已售完
2020-07-06 16:50:59.346098+0800 GCD[3445:1211593] 所有火车票均已售完
2020-07-06 16:50:59.352441+0800 GCD[3445:1211597] 所有火车票均已售完
2020-07-06 16:50:59.352441+0800 GCD[3445:1211596] 所有火车票均已售完
2020-07-06 16:50:59.355088+0800 GCD[3445:1211592] 所有火车票均已售完

在不考虑线程安全,不使用 semaphore 的情况下,得到票数是错乱的。

相关文章

网友评论

      本文标题:多线程售卖火车票(非线程安全)

      本文链接:https://www.haomeiwen.com/subject/nehgqktx.html