将子线程里的任务放入串行队列,也可以保证不同子线程里的任务不会同时执行,也可以解决线程同步问题。
    卖票和存钱/取钱问题:

    1. - (instancetype)init {
    2. if (self = [super init]) {
    3. self.ticketQueue = dispatch_queue_create("ticketQueue", DISPATCH_QUEUE_SERIAL);
    4. self.moneyQueue = dispatch_queue_create("moneyQueue", DISPATCH_QUEUE_SERIAL);
    5. }
    6. return self;
    7. }
    8. - (void)__saleTicket {
    9. dispatch_sync(self.ticketQueue, ^{
    10. [super __saleTicket];
    11. });
    12. }
    13. - (void)__saveMoney {
    14. dispatch_sync(self.moneyQueue, ^{
    15. [super __saveMoney];
    16. });
    17. }
    18. - (void)__drawMoney {
    19. dispatch_sync(self.moneyQueue, ^{
    20. [super __drawMoney];
    21. });
    22. }