Three ways to create multithreading through GCD, nsoperationqueue queues, Nsthread

Source: Internet
Author: User
Tags gcd

#import "ViewController.h"@interfaceViewcontroller () @property (weak, nonatomic) Iboutlet UILabel*Remindlabel;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}-(Ibaction) Sendbtnclick: (UIButton *) Sender {/*--First use GCD to send requests and refresh ui--*/Dispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{sleep (2); Dispatch_async (Dispatch_get_main_queue (),^{_remindlabel.text=@"sent successfully";           });     }); /*--The second uses nsblockoperation, Nsoperationqueue queue to send requests and refresh ui--*/Nsoperationqueue*myqueue=[[Nsoperationqueue alloc]init]; Nsblockoperation*operation=[nsblockoperation blockoperationwithblock:^{       //2 seconds DelaySleep2); //main thread Refresh UIDispatch_async (Dispatch_get_main_queue (), ^{_remindlabel.text=@"sent successfully";    });    }];            [Myqueue addoperation:operation]; /*--The third uses Nsthread to send requests and refresh ui--*/[Nsthread detachnewthreadselector: @selector (sendstate) totarget:self Withobject:nil]; }-(void) sendstate{Sleep (2); Dispatch_async (Dispatch_get_main_queue (),^{[Self updatelabel]; });}-(void) updatelabel{_remindlabel.text=@"sent successfully";}@end

Three ways to create multithreading through GCD, nsoperationqueue queues, Nsthread

Related Article

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.