iOS 多线程实例(NSThread售票窗口的模拟)

主要的知识点:

初始化:NSThread *thirdtWindow = [[NSThread alloc]initWithTarget:selfselector:@selector(saleTicket) object:nil];

    thirdtWindow.name = @"售票窗口";

    [thirdtWindow start];

NSCondition *condition = [[NSCondition alloc]init];

上锁:[condition lock];

解锁:[condition unlock];

#import "ViewController.h"

@interface ViewController ()

{

    UILabel *showLabel;

    int curTicketNum;

    int saleTicketNum;

    

    NSString *saleWindowName;

    

    NSCondition *condition;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

//    卖票系统分三个窗口 同时销售

    curTicketNum = 100;

    

    UIButton *button = [UIButton buttonWithType:0];

    button.frame = CGRectMake(100, 20, 150, 30);

    [button setTitle:@"开始卖票" forState:UIControlStateNormal];

    button.backgroundColor = [UIColor blackColor];

    [button addTarget:self action:@selector(startSale) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    

    showLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, CGRectGetWidth(self.view.frame), CGRectGetWidth(self.view.frame))];

    showLabel.backgroundColor = [UIColor whiteColor];

    showLabel.text = @"剩余100张票";

    showLabel.numberOfLines = 3;

    showLabel.textAlignment = NSTextAlignmentCenter;

    [self.view addSubview:showLabel];

}

//三个窗口同时卖票

- (void)startSale

{

//初始化三个线程每一个线程是一个售票窗口

    NSThread *firstWindow = [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil];

    firstWindow.name = @"售票窗口1";

    [firstWindow start];

    

    NSThread *twoWindow = [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil];

    twoWindow.name = @"售票窗口2";

    [twoWindow start];

    

    NSThread *thirdtWindow = [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil];

    thirdtWindow.name = @"售票窗口3";

    [thirdtWindow start];

    

   condition = [[NSCondition alloc]init];

    

}

- (void)saleTicket

{

    while (curTicketNum > 0) {

        [NSThread sleepForTimeInterval:0.3];

        [condition lock];

//    当前票数

        curTicketNum -= 1;

//       卖得票数

        saleTicketNum = 100 - curTicketNum;

        saleWindowName = [NSThread currentThread].name;

        if (curTicketNum > 0) {

             [condition unlock];

        }

        [self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:YES];

    }

}

- (void)updateUI

{

    NSLog(@"已经销售%d张票\n还剩%d张票",saleTicketNum,curTicketNum);

    showLabel.text = [NSString stringWithFormat:@"%@\n已经销售%d张票\n还剩%d张票",saleWindowName,saleTicketNum,curTicketNum];

}

@end


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值