Phần giao diện mô tả. Lưu các thay đổi trong giao diện Builder, và chúng ta hãy tập trung vào phần thực hiện của ThreadingViewController chúng tôi. Mở tập tin trong Xcode. Chúng ta cần để hoàn thành triển khai thực hiện phương pháp setter getter của chúng tôi và. | 72 CHAPTER 3 Doing Several Things at Once Performance Enhancements with Threading Figure 3-15. Connect the events to the associated buttons. Thread Count 0 Thread Count 0 Start Counting 1 Start Counting 2 Thread Count 0 Thread Count 0 Start Counting 3 start Counting 4 Creating a Thread The interface section is now described. Save the changes in Interface Builder and let s focus on the implementation section of our ThreadingViewController. Open the file in Xcode. We need to complete implementing our getter and setter methods. To do so add the code in Listing 3-2 to the implementation section of the file. Listing 3-2. Completing the Getter and Setter Methods in synthesize totalcount synthesize threadlLabel synthesize thread2Label synthesize thread3Label synthesize thread4Label synthesize buttonlTitle synthesize button2Title synthesize button3Title synthesize button4Title synthesize updatedByThread CHAPTER 3 Doing Several Things at Once Performance Enhancements with Threading 73 We need to look at how to implement our thread when a user wants to launch a thread by clicking a button. Clicking a Start Counting button will trigger an event that will be handled by the corresponding launchThreadX methods. Creating a Worker Thread It is important now to discuss how we are going to implement a worker thread and what the thread is going to do. When the thread is spawned it increments a counter ten times updates its display on the iPhone sleeps after each increment and updates the total thread counter at the end of each cycle. The cycle repeats until the user signals to kill the individual thread or kill all threads by clicking Stop All Counting . See Figure 3-16. performSelectorInBackground @selector thread1 withObject nil thread1 Thread the Needle App performSelectorOnMainThread @selector displayThread1Counts withObject myNumber waitUntilDone YES while button1On I I --T--------------------------I .