IPhone multi-thread programming experience
Anonymous Internet comments (0) font size: T | T
Multi-threaded programming is the best way to prevent the main thread from being congested and increase the running efficiency. The original multi-threaded method has many problems, including thread locking.
AD:
I found the multi-thread programming content of the IPhone for a long time. The class used is NSThread in UIKit ..
During the google process, I found many articles with the title similar to <multi-threaded OS4 comes>. These ideas provide a positive guidance. The highlight of 0S4 is multitasking. A task is a process, it is also called multi-process, and multithreading was available on early iPhone OS.
In iphone OS, the concept of a task is an application. You can only do one thing at a time, that is, you cannot play games at the same time, while playing QQ at the same time.
The process is as follows:
1. Create a thread
[NSThread detachNewThreadSelect: @ selector (BeginThread)
ToTarget: selft
WithObject: nil];
2. Do two jobs in the thread. One is dosomethinglongtime in the background processing, and the other is UpdateUI)
View plaincopy to clipboardprint?
(Void) BeginThread {
[Self defined mselectorinbackgroud: @ selector (dosomethinglongtime)
WithObject: nil];
[Self perfomselecw.mainthread: @ selector (UpdateUI)
WithObject: nil
WatUntilDone: NO];
}
3. How does UpdateUI data come from?
View plaincopy to clipboardprint?
-(Void) dosomethinglongtime {
// Modify the shared variables varProgress, varText, and so on.
}
{Void) UpdateUI {
// Obtain the shared variables varProgress and varText, which are displayed on the interface.
}
This completes a general process, but the while (1) cannot be used in UpdateUI. Otherwise, the main thread will be blocked in the UpdateUI function. What should I do? Google has a method, and the UpdateUI method has been modified.
This means that if no thread is finished, the function will return to the function update interface after 0.2 seconds, so the loop ends until the end.
View plaincopy to clipboardprint?
(Void) UpdateUI {
// Obtain the shared variables varProgress and varText, which are displayed on the interface.
If (! Finished)
[NSTimer scheduledTimerWithTimeInterval: 0.2 target: self
Selector: @ selector (UpdateUI) userInfo: nil repeats: NO];
}
One of the above methods of multi-thread programming for the IPhone, of course, some people suggest using NSOperation and NSOperationQueue, but I tried it and didn't try it out. I will try it again when there is a result.