iOS-多线程(模拟火车票售票系统)

关于线程的介绍见上一片博文,iOS单线程:http://blog.csdn.net/yakerwei/article/details/17589709

一、实现结果

在本程序中,用到7个控件,三个说明性label,三个输出口label,一个button。


二、代码的属性部分

用到的属性:3个输出label,一个按钮

一个按钮方法

#import <UIKit/UIKit.h>


@interface WeSecondThreadViewController : UIViewController

{   //剩余票数和售出票数

    int _leftTicks;

    int _saledTicks;

    //两个线程

    NSThread *_firstThread;

    NSThread *_secondThread;

    //lock条件

    NSCondition *_ticksConfition;

}

@property (retain, nonatomic) IBOutlet UILabel *leftTicksLabel;

@property (retain, nonatomic) IBOutlet UILabel *saledTicksLabel;

@property (retain, nonatomic) IBOutlet UILabel *currentThreadLabel;

@property (retain, nonatomic) IBOutlet UIButton *startBtn;

- (IBAction)startAction:(id)sender;


@end

三、方法实现部分

//剩余票数和售出票数初始化

- (void)viewDidLoad

{

    [super viewDidLoad];

    _leftTicks = 100;

    _saledTicks = 0;

    _ticksConfition = [[NSCondition alloc]init];

    // Do any additional setup after loading the view from its nib.

}

//按钮的响应方法

- (IBAction)startAction:(id)sender

{

    //线程一开始

    _firstThread = [[NSThread alloc]initWithTarget:self selector:@selector(startThread:) object:nil];

    [_firstThread setName:@"Thread_1"];

    [_firstThread start];

    //线程二开始

    _secondThread = [[NSThread alloc]initWithTarget:self selector:@selector(startThread:) object:nil];

    [_secondThread setName:@"Thread_2"];

    [_secondThread start];

}

- (void)startThread:(id)sender

{

   while (TRUE)

   {

       //线程锁,使一二线程交互

       [_ticksConfition lock];

       if (_leftTicks > 0 )

       {

           [NSThread sleepForTimeInterval:0.1];

           _leftTicks --;

           _saledTicks = 100 - _leftTicks;

           NSString *pstr = [[NSThread currentThread]name];

           NSLog(@"售出票数:%i 剩余票数%i 当前线程%@",_saledTicks,_leftTicks,pstr);

       }

       else if (_leftTicks == 0)

       {

           NSLog(@"票已售完");

           break;

       }

       //在主线程中更新

       [self performSelectorOnMainThread:@selector(updateMyView:) withObject:[[NSThread currentThread]name] waitUntilDone:YES];

       //解锁

       [_ticksConfition unlock];

   }

}

//屏幕上更新

- (void)updateMyView:(id)sender

{

   self.leftTicksLabel.text = [NSString stringWithFormat:@"%i",_leftTicks];

   self.saledTicksLabel.text = [NSString stringWithFormat:@"%i",_saledTicks];

   self.currentThreadLabel.text = (NSString*)sender;

    

    if (_leftTicks == 0)

    {

        UIAlertView *pAlter = [[UIAlertView alloc]initWithTitle:@"通知" message:@"今日票已售完" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确认", nil];

        [pAlter show];

        [pAlter release];

    }


}

相关demo下载地址:http://download.csdn.net/detail/u012887301/6777319

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值