iOS-多线程(模拟火车票售票系统)
2013-12-27 15:37
405 查看
关于线程的介绍见上一片博文,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];
}
}
一、实现结果
在本程序中,用到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
相关文章推荐
- 同步与互斥经典案例——模拟火车票售票系统
- Java多线程4—线程同步问题+火车票售票系统
- java多线程之火车售票系统模拟
- 多线程模拟售票系统
- 多线程火车票售票系统——人工重置事件对象实现线程同步(会有问题的)
- 多线程模拟售票系统2
- 多线程火车票售票系统——自动重置事件对象实现线程同步
- iOS学习笔记——多线程(简单售票系统)
- 模拟火车票购票系统的简单多线程程序
- 模拟火车票售票(多线程)
- java 多线程模拟售票系统
- 多线程火车票售票系统——临界区实现线程同步
- 关于多线程实现火车票售票模拟
- 用java多线程操作模拟售票系统
- 多线程火车票售票系统——互斥对象实现线程同步
- 设计一个多线程程序如下:设计一个火车售票模拟程序。假如火车站要有100张火车票要卖出,现在有5个售票点同时售票,用5个线程模拟这5个售票点的售票情况(提示:利用多线程)
- 将线程锁加在对象上与锁加在方法上的区别(模拟火车票联网售票系统:多个线程同时出票,保证每张出票的编号连续且不重复。)
- App11_05_Runnable接口程序模拟航班售票系统
- 如果你是来12306系架,你如何实现? ——关于构建安全、稳定、高吞吐量的火车票网络售票系统几个方面(2)结束及总结
- 火车票系统模拟