Multi-threaded use of the real column-ticketing system

Source: Internet
Author: User



#import "ViewController.h"


@interface Viewcontroller ()

{

UILabel *showlable;

int curticketnum;

int saleticketnum;

NSString *salewindowname;

Nscondition *conditionclock;

}

@end


@implementation Viewcontroller


-(void) Viewdidload {

[Super Viewdidload];

// ticketing system divided into 3 windows sold simultaneously

default of one ticket

Curticketnum = 100;

UIButton *button = [UIButton buttonwithtype:uibuttontypecustom];

Button.frame = CGRectMake (100, 20, 150, 50);

[Self.view Addsubview:button];

[Button settitle:@ " Sell ticket " forstate:uicontrolstatenormal];

Button.backgroundcolor = [Uicolor Redcolor];

[Button addtarget:self action: @selector (Startsale) forcontrolevents:uicontroleventtouchupinside];


showlable = [[UILabel alloc]initwithframe:cgrectmake (0, 200, 375, 200)];

[Self.view addsubview:showlable];

Showlable.numberoflines = 3;

Showlable.textalignment = Nstextalignmentcenter;

Showlable.text = @ " remaining votes:+" ;

Showlable.font = [Uifont boldsystemfontofsize:20];

Showlable.backgroundcolor = [Uicolor Orangecolor];

}


// three windows to sell tickets at the same time

-(void) Startsale

{

// Initialize 3 threads Each of the threads is a window

Nsthread *firstwindow = [[Nsthread alloc]initwithtarget:self selector: @selector (saleticket) Object:nil];

Firstwindow.name = @ " ticketing < window 1>";

[Firstwindow start];

Nsthread *secondtwindow = [[Nsthread alloc]initwithtarget:self selector: @selector (saleticket) Object:nil];

Secondtwindow.name = @ " ticketing < window 2>";

[Secondtwindow start];

Nsthread *thirdwindow = [[Nsthread alloc]initwithtarget:self selector: @selector (saleticket) Object:nil];

Thirdwindow.name = @ " ticketing < window 3>";

[Thirdwindow start];

Nscondition is a wire lock (conditional lock)

Conditionclock = [[Nscondition alloc]init];

}



-(void) Saleticket

{

// not using the thread lock 3 threads (Windows) will also access the method of selling tickets when the remaining number of votes is 0 , other threads will continue to visit, not knowing that the remaining number of votes is 0 , continue to access the remaining number of votes there is a negative situation

// solve this problem can use the thread lock; only one thread is allowed to access it after the other thread accesses it.

while (Curticketnum > 0) {

// Use the thread lock to allow only one thread to fetch access

[Conditionclock Lock];

Salewindowname = [Nsthread currentthread].name;

[Nsthread sleepfortimeinterval:0.1];

current number of votes ( remaining )

Curticketnum-= 1;

number of votes sold

Saleticketnum = 100-curticketnum;

[Self Performselectoronmainthread: @selector (UpdateUI) Withobject:nil Waituntildone:yes];

if (Curticketnum > 0) {

[Conditionclock unlock];

}

}

}



// Update interface

-(void) UpdateUI

{

NSLog (@ " already sold %d tickets \ n The remaining %d tickets \ n The current sales window is:%@", Saleticketnum,curticketnum, Salewindowname);

Showlable.text = [NSString stringwithformat:@ " has sold %d tickets \ nthe remaining %d tickets \ n The current sales window is: %@ ", Saleticketnum,curticketnum,salewindowname];

}



-(void) didreceivememorywarning {

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}


@end


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Multi-threaded use of the real column-ticketing system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.