The principle and demonstration of iOS multithreading development-nsthread

Source: Internet
Author: User







//Create thread mode 1
- (
void) test1
{
//Instantiate a thread pair like nsthread *thread = [[nsthreadalloc] initwithtarget: Self Selector:@selector(Run) object:nil];    //Let the thread start working, start the thread, execute the Run method on the newly opened thread[Threadstart];}
//Create thread mode 2
- (
void) test2
{
NSLog(@"---%@", [NsthreadCurrentThread]);[nsthreaddetachnewthreadselector:@selector(run:)totarget: Self withobject:@ "Hello"];}
//Create thread mode 3
- (
void) test3
{
//"Implicit" Create thread mode
[
SelfPerformselectorinbackground:@selector(run:)Withobject:@ "CZ"];}
#pragma mark-the properties of a thread
- (void) test4
{
nsthread *threada = [[nsthreadalloc] initwithtarget: Self Selector:@selector(run:) Object:@ "Hello"];Threada.name= @ "Thraed A";
    //Thread Priority
//is a floating-point number, 0.0~1.0. Default value 0.5
//development, generally do not change the value of the priority level.
//priority, which must be called many times before it can be manifested. Threada. threadpriority =0.1;
    //start work[Threadastart];}

//Nsthread The state of the thread
- (void) Run
{
NSLog(@ "%s", __func__);

//Just come in and sleep for 2 seconds.
//[Nsthread sleepfortimeinterval:5.0];

//Sleep to a specified point in time
//[Nsthread sleepuntildate:[nsdate datewithtimeintervalsincenow:5.0];

for (inti = 0; i < -; i++) {

after a certain condition is met, the execution of the thread is blocked. That is, let the thread take a break .
if (i = =Ten) {
[
NsthreadSleepfortimeinterval:3.0];
}

//Once a condition is reached, the execution of the thread is forced to terminate
if (i = = the) {
//Once the force is terminated, it cannot be restarted
//Once forced termination, the following code will not execute
[
NsthreadExit];
}

NSLog(@ "%@---%d", [NsthreadCurrentThread], i);
}

NSLog(@ "Thread End");
}

   //Upload data to the main thread and update the UI on the main thread 1 .[ SelfPerformselectoronmainthread:@selector(downloadfinish:)Withobject: ImageWaituntildone:NO];
    2. [ SelfPerformselector:@selector(downloadfinish:) onthread:[nsthread Mainthread] withobject:image waituntildone:NO];
3.[self. IconView performselectoronmainthread:@selector(setimage:) Withobject: Image waituntildone:YES];

//nonatomic non-atomic properties
//Atomic Atomic Properties--Default Properties
//Atomic properties are designed for multithreading. Atomic properties implement single (thread) write multiple (thread) Read//Because the level of security required for writing is higher. Read the requirements lower, you can read several times to ensure the correctness of the data
    //Atomic properties internal use of spin lock
//Spin lock and mutual exclusion lock
//Common: You can lock a piece of code. At the same time, only the thread can execute this locked code
//difference: Mutex, when locked, other threads will sleep, wait for the condition to meet, then wake up //Spin lock, when locked, other threads will do a dead loop, waiting for this condition to meet, once the condition is met, immediately to execute, less a wake-up process

The principle and demonstration of iOS multithreading development-nsthread

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.