下面介绍一个运用多线程的简单售票系统,代码是在上一篇文章的基础上进行操作的。
首先,创建一个继承UIViewController的类YueTwoViewController,然后在YueAppDelegate.m中添加代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
YueViewController *viewController = [[YueViewController alloc]initWithNibName:nil bundle:nil];
YueTwoViewController *pTwoVC = [[YueTwoViewController alloc]initWithNibName:nil bundle:nil];
UITabBarController *pTabbarVC = [[UITabBarController alloc]init];
pTabbarVC.viewControllers = [NSArray arrayWithObjects:viewController,pTwoVC,nil];
//设置窗口的根视图
self.window.rootViewController = pTabbarVC;
[pTabbarVC release];
[viewController release];
[pTwoVC release];
[pThirdVC release];
[self.window makeKeyAndVisible];
return YES;
}
在YueTwoViewController.xib文件中拖拽控件,并将它们关联到YueTwoViewController.h中
在YueTwoViewController.m中进行方法的实现和操作
#import "YueTwoViewController.h"
@interface YueTwoViewController ()
@end
@implementation YueTwoViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tabBarItem.title = @"Thread-2";
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//设置剩余票的数量
_lefttickets = 100;
//设置卖出的票数
_saletickets = 0;
_ticketsCondition = [[NSCondition alloc]init];
//创建一个Button,并对其进行相应的操作,关联一个方法
UIButton *pBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[pBtn setFrame:CGRectMake(100, 360, 80, 40)];
[pBtn setTitle:@"Start" forState:UIControlStateNormal];
[pBtn addTarget:self action:@selector(threadStart:)forControlEvents:UIControlEventTouchUpInside];
//添加到当前视图上
[self.view addSubview:pBtn];
}
- (void)threadStart:(id)sender
{
//创建第一个子线程
_firstThread = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:nil];
//设置子线程的名字
[_firstThread setName:@"Thread-One"];
//开始子线程
[_firstThread start];
_secondThread = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:nil];
[_secondThread setName:@"Thread_Two"];
[_secondThread start];
_thirdThread = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:nil];
[_thirdThread setName:@"Thread_Three"];
[_thirdThread start];
}
- (void)run:(id)sender
{
while (TRUE)
{
[_ticketsCondition lock];
if (_lefttickets > 0)
{
[NSThread sleepForTimeInterval:0.1];
_lefttickets--;
_saletickets = 100 - _lefttickets;
NSString *pSta = [[NSThread currentThread]name];
NSLog(@"售出票数:%i,剩余票数:%i,当前票数:%@",_saletickets,_lefttickets,pSta);
}
else if (_lefttickets == 0)
{
NSLog(@"票已售完");
break;
}
[self performSelectorOnMainThread:@selector(updateMyView:) withObject:[[NSThread currentThread]name] waitUntilDone:YES];
[_ticketsCondition unlock];
}
}
- (void)updateMyView:(id)sender
{
self.labelleft.text = [NSString stringWithFormat:@"%i",_lefttickets];
self.labelsale.text = [NSString stringWithFormat:@"%i",_saletickets];
self.currentThread.text = (NSString *)sender;
if (_lefttickets == 0)
{
UIAlertView *pAlertView = [[UIAlertView alloc]initWithTitle:@"通知" message:@"今日票已售完" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
[pAlertView show];
[pAlertView release];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[_ticketsCondition release];
[_labelsale release];
[_labelleft release];
[_currentThread release];
[super dealloc];
}
@end
运行后,就可以观察子线程的顺行情况了。