iOS Learning note 31-Multithreading in depth

Source: Internet
Author: User

/**

* Create a queue (serial)

**/

dispatch_queue_t queue1 = dispatch_queue_create ("queue1", dispatch_queue_serial);

/**

* Add the path of the code block execution thread

**/

Dispatch_async (Queue1, ^{

for (int i = 0; i<100; i++) {

NSLog (@ "line Cheng,%d", i);

}

});

Create a thread in the form of a C-language function

Dispatch_sync_f (queue1, NULL, treadfun);//using thread functions as the path to the thread

void Treadfun () {

for (int i =0; i<100; i++) {

NSLog (@ "Thread 2,%d", i);

}

}

dispatch_time_t time = Dispatch_time (Dispatch_time_now, 2*nsec_per_sec);

dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);

Dispatch_after (time, queue, ^{

NSLog (@ "222");

//    });

The above code is equivalent to

NSLog (@ "11111");

Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{

NSLog (@ "2222222");

});

/**

* Executed after a period of time

1,dispatch_after

2,nsobject Performselector

3,timer Timer method

**/

[Super Viewdidload];

Nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:5.0 target:self selector: @selector (myfun) Userinfo:nil Repeats:yes];

[Timer invalidate];

[Self performselector: @selector (myfun) Withobject:nil afterdelay:5.0];

iOS Learning note 31-Multithreading in depth

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.