美文网首页
NSThread的使用(买票实例)

NSThread的使用(买票实例)

作者: 番薯大佬 | 来源:发表于2018-03-06 15:11 被阅读7次
// 剩余票数
NSInteger ticketCount = 10;
// 购票窗口
- (void)buyTicket
{
    NSThread *window1 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    window1.name = @"深圳北站";
    [window1 start];
    
    NSThread *window2 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    window2.name = @"深圳福田站";
    [window2 start];
    
    ticketCount = 10;
}
// 购票-加锁
- (void)saleTicket
{
    while (1)
    {
        // 互斥锁
        @synchronized (self) {
            if (ticketCount > 0)
            {
                // 如果还有票,继续售卖
                NSInteger count = arc4random() % 3 + 1; // 在窗口购买任意n+1张票
                if (ticketCount == 1)
                {
                    count = 1;
                }
                ticketCount -= count; // 剩余票数
                NSLog(@"在 %@ 窗口买了 %ld 张,还剩下 %ld 张。", [NSThread currentThread].name, count, ticketCount);
                
                [NSThread sleepForTimeInterval:0.2];
            }
            else
            {
                // 如果已卖完,关闭售票窗口
                if ([NSThread currentThread].isCancelled)
                {
                    NSLog(@"关闭 %@ 窗口", [NSThread currentThread].name);
//                    [NSThread exit]; // 终止线程
                    break;
                }
                else
                {
                    NSLog(@"在 %@ 窗口买票时,没有票了。", [NSThread currentThread].name);
                    
                    // 给当前线程标记为取消状态
                    [[NSThread currentThread] cancel];
                    // 停止当前线程的runLoop
                    CFRunLoopStop(CFRunLoopGetCurrent());
                }
            }
        }
    }
}
// 加锁情况下购票,执行结果正常
2018-03-06 14:53:39.861 DemoThread[6605:1274282] 在 深圳北站 窗口买了 1 张,还剩下 9 张。
2018-03-06 14:53:40.063 DemoThread[6605:1274283] 在 深圳福田站 窗口买了 3 张,还剩下 6 张。
2018-03-06 14:53:40.266 DemoThread[6605:1274282] 在 深圳北站 窗口买了 2 张,还剩下 4 张。
2018-03-06 14:53:40.471 DemoThread[6605:1274283] 在 深圳福田站 窗口买了 2 张,还剩下 2 张。
2018-03-06 14:53:40.674 DemoThread[6605:1274282] 在 深圳北站 窗口买了 1 张,还剩下 1 张。
2018-03-06 14:53:40.879 DemoThread[6605:1274283] 在 深圳福田站 窗口买了 1 张,还剩下 0 张。
2018-03-06 14:53:41.085 DemoThread[6605:1274282] 在 深圳北站 窗口买票时,没有票了。
2018-03-06 14:53:41.085 DemoThread[6605:1274283] 在 深圳福田站 窗口买票时,没有票了。
2018-03-06 14:53:41.085 DemoThread[6605:1274282] 关闭 深圳北站 窗口
2018-03-06 14:53:41.086 DemoThread[6605:1274283] 关闭 深圳福田站 窗口
// 购票-未加锁
- (void)saleTicket
{
    while (1)
    {
            if (ticketCount > 0)
            {
                // 如果还有票,继续售卖
                NSInteger count = arc4random() % 3 + 1; // 在窗口购买任意n+1张票
                if (ticketCount == 1)
                {
                    count = 1;
                }
                ticketCount -= count; // 剩余票数
                NSLog(@"在 %@ 窗口买了 %ld 张,还剩下 %ld 张。", [NSThread currentThread].name, count, ticketCount);
                
                [NSThread sleepForTimeInterval:0.2];
            }
            else
            {
                // 如果已卖完,关闭售票窗口
                if ([NSThread currentThread].isCancelled)
                {
                    NSLog(@"关闭 %@ 窗口", [NSThread currentThread].name);
//                    [NSThread exit]; // 终止线程
                    break;
                }
                else
                {
                    NSLog(@"在 %@ 窗口买票时,没有票了。", [NSThread currentThread].name);
                    
                    // 给当前线程标记为取消状态
                    [[NSThread currentThread] cancel];
                    // 停止当前线程的runLoop
                    CFRunLoopStop(CFRunLoopGetCurrent());
                }
            }
    }
}
// 未加锁情况下,购买异常
2018-03-06 14:58:35.457 DemoThread[6648:1278848] 在 深圳北站 窗口买了 3 张,还剩下 7 张。
2018-03-06 14:58:35.457 DemoThread[6648:1278849] 在 深圳福田站 窗口买了 2 张,还剩下 5 张。
2018-03-06 14:58:35.661 DemoThread[6648:1278849] 在 深圳福田站 窗口买了 2 张,还剩下 0 张。
2018-03-06 14:58:35.661 DemoThread[6648:1278848] 在 深圳北站 窗口买了 3 张,还剩下 2 张。
2018-03-06 14:58:35.864 DemoThread[6648:1278848] 在 深圳北站 窗口买票时,没有票了。
2018-03-06 14:58:35.864 DemoThread[6648:1278849] 在 深圳福田站 窗口买票时,没有票了。
2018-03-06 14:58:35.864 DemoThread[6648:1278848] 关闭 深圳北站 窗口
2018-03-06 14:58:35.864 DemoThread[6648:1278849] 关闭 深圳福田站 窗口

相关文章

  • NSThread的使用(买票实例)

  • iOS开发中多线程基本使用简介

    一)NSThread 的使用 NSThread 有两种创建方式第一种是实例方法,第二种是类方法: 参数的意义:se...

  • Objc多线程-NSThread

    NSThread 查看NSThread.h中的接口: 类方法不返回NSThread实例,直接在其他线程里面执行任务...

  • 关于线程三种方式的比较

    一、Thread主要利用NSThread这个类,一个NSThread实例代表着一条线程1、优点:NSThread比...

  • iOS NSThread 类

    NSThread 官方文档 initWithTarget:selector:object:(实例方法) detac...

  • iOS_多线程

    NSThread NSThread是轻量级的多线程开发,使用起来也并不复杂,但是使用NSThread需要自己管理线...

  • iOS 多线程

    NSThread 使用NSThread对象建立一个线程非常方便,但要使用NSThread管理多个线程较困难,不推荐...

  • iOS 多线程(一)---> NSThread

    NSThread的使用 No.1:NSThread创建线程 NSThread有三种创建方式: init方式 det...

  • 多线程二(NSThread)

    一个NSThread实例代表一条线程(了解,较少使用) 初始化 动态,需要start开启 静态,直接开启 隐式创建...

  • iOS 多线程

    iOS使用线程的方式 pthread NSThread GCD NSOperation NSThread线程的创建...

网友评论

      本文标题:NSThread的使用(买票实例)

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