Network Start---Multithreading---thread security issues (understanding) (iv)

Source: Internet
Author: User
Tags mutex ticket

1 //2 /**3 * Thread's note point4 1. Do not open too many threads at the same time (1-3, not more than 5)5  6 Threading Concepts:7 1. Main thread: UI thread, display, refresh UI interface, handle events for UI controls8 2. Child threads (asynchronous threads, background threads)9  Ten 3. Do not place time-consuming operations on the main thread, but on sub-threads One   A   - Here's a case of 3-window ticket sales. -  */ the  - #import "HMViewController.h" -  - @interfaceHmviewcontroller () + //3 Windows 3 Threads -@property (nonatomic, strong) Nsthread *Thread1; +@property (nonatomic, strong) Nsthread *thread2; A@property (nonatomic, strong) Nsthread *thread3; at  - /** - * Number of votes left -  */ -@property (nonatomic, assign)intLeftticketcount; -  in  - //lock the object, with a lock, do not change the lock frequently, to ensure the security of the thread to //@property (nonatomic, strong) NSObject *locker; + @end -  the @implementationHmviewcontroller *  $- (void) ViewdidloadPanax Notoginseng { - [Super Viewdidload]; the      +     //the number of votes left to begin is 50 sheets. ASelf.leftticketcount = -; the      +     //Creating Threads -Self.thread1 = [[Nsthread alloc] initwithtarget:self selector: @selector (Saleticket)Object: nil]; $     //Thread Name $Self.thread1.name =@"Window No. 1th"; -      -Self.thread2 = [[Nsthread alloc] initwithtarget:self selector: @selector (Saleticket)Object: nil]; theSelf.thread2.name =@"window No. 2nd"; -     WuyiSelf.thread3 = [[Nsthread alloc] initwithtarget:self selector: @selector (Saleticket)Object: nil]; theSelf.thread3.name =@"Window No. 3rd"; -      Wu //self.locker = [[NSObject alloc] init]; - } About  $ //Click Start Thread 3 windows and start selling tickets, -- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event - { - [self.thread1 start]; A [self.thread2 start]; + [self.thread3 start]; the } -  $ /** the * Ticket sales the  */ the- (void) Saleticket the { -      while(1) {//Write a dead loop, sell it, sell it. in          the         //() enclosed in parentheses are the lock object brackets inside with self on the line, marking the only one lock the          About@synchronized (self) {//starting a lock is called a mutex . the              the             //ensure that a thread accesses a resource while other threads cannot access the resource, so long as the @synchronized keyword is used to secure the thread the              +             //multiple threads need to lock when they rob the same piece of resources, otherwise it is very CPU intensive -             //thread synchronization: Sequential execution of tasks, thread synchronization issues lock to solve problems the             //Mutex is the use of thread synchronization technologyBayi              the             //Take out the remaining number of votes the             intCount =Self.leftticketcount; -             //if the number of votes is greater than 0 -             if(Count >0) { the[Nsthread Sleepfortimeinterval:0.05]; the                  the                 //number of votes left -1 theSelf.leftticketcount = count-1; -                  theNSLog (@"%@ sold a ticket and%d tickets left.", [Nsthread currentthread].name, self.leftticketcount); the}Else{//if the number of votes is 0, exit the loop the                 94                 return;//Exit Loop the                  the             } the}//Unlock98     } About } - 101 @end

Network Start---Multithreading---thread security issues (understanding) (iv)

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.