IPhone multi-thread programming experience

Source: Internet
Author: User

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.

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.