Network start --- multithreading --- thread security (understanding) (4), --- Multithreading
1 // 2/** 3 * pay attention to thread 4 1. do not enable too many threads at the same time. (1-3 threads can be used. A maximum of 5 threads can be used.) concept of 6 threads: 7 1. main thread: UI thread, display and refresh the UI interface, and process the events of the UI control 8. sub-thread (asynchronous thread, background thread) 9 10 3. do not place time-consuming operations in the main thread. In the Sub-thread, execute 11 12 13. Here are three window ticket selling cases 14 */15 16 # import "HMViewController. h "17 18 @ interface HMViewController () 19 // 3 windows 3 threads 20 @ property (nonatomic, strong) NSThread * thread1; 21 @ property (nonatomic, strong) NSThread * thread2; 22 @ property (nonatomic, strong) NSThread * thread3; 23 24/** 25*26 */27 @ property (nonatomic, assign) int leftTicketCount; 28 29 30 // Lock Object, use a lock. Do not change the lock frequently to ensure thread security 31 // @ property (nonatomic, strong) NSObject * locker; 32 @ end 33 34 @ implementation HMViewController 35 36-(void) viewDidLoad 37 {38 [super viewDidLoad]; 39 40 // the remaining number of votes at the beginning is 50 41 self. leftTicketCount = 50; 42 43 // create thread 44 self. thread1 = [[NSThread alloc] initWithTarget: self selector: @ selector (saleTicket) object: nil]; 45 // thread name 46 self. thread1.name = @ "Window 1"; 47 48 self. thread2 = [[NSThread alloc] initWithTarget: self selector: @ selector (saleTicket) object: nil]; 49 self. thread2.name = @ "window 2"; 50 51 self. thread3 = [[NSThread alloc] initWithTarget: self selector: @ selector (saleTicket) object: nil]; 52 self. thread3.name = @ "window 3"; 53 54 // self. locker = [[NSObject alloc] init]; 55} 56 57 // click Start thread to start selling tickets in three windows at the same time, 58-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event 59 {60 [self. thread1 start]; 61 [self. thread2 start]; 62 [self. thread3 start]; 63} 64 65/** 66 * sell tickets 67 */68-(void) saleTicket 69 {70 while (1) {// write an endless loop to keep selling, sell light until 71 72 // () the Lock Object is placed in the brackets with self on the line, marking the only lock 73 74 @ synchronized (self) {// The start lock is called the mutex lock 75 76 // when a thread accesses the resource, other threads cannot access the resource, as long as the @ synchronized keyword is used, ensure thread security 77 78 // lock is required only when multiple threads snatch the same resource; otherwise, CPU resources are very consumed 79 // thread synchronization: execute tasks in order, thread Synchronization locks to Solve the Problem 80 // The mutex lock uses the thread synchronization technology 81 82 // get the remaining number of votes 83 int count = self. leftTicketCount; 84 // if the number of votes is greater than 0 85 if (count> 0) {86 [NSThread sleepForTimeInterval: 0.05]; 87 88 // remaining votes-1 89 self. leftTicketCount = count-1; 90 91 NSLog (@ "% @ sold a ticket, % d remaining tickets", [NSThread currentThread]. name, self. leftTicketCount); 92} else {// if the number of votes is 0, exit loop 93 94 return; // exit loop 95 96} 97} // unlock 98} 99} 100 101 @ end