Commonly used time-lapse execution

Source: Internet
Author: User

1, Performselector once custom delay task, will not get stuck in the current thread

// call the self's Run method after 2 seconds [Self performselector: @selector (Run) Withobject:nil Afterdelay:2.0];

2. Using the GCD function

// in this method, the incoming queue is used to determine which thread the block executes   in. // If the host column is passed in, the block is  called on the main thread // If a global queue is passed in, the block is called  in the child thread Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (3.0 * nsec_per_sec)), Dispatch_get_global_queue (00//  Here the incoming global queue executes the block in the child thread, and if the incoming host column executes the block      in the main thread NSLog (@ "3 seconds after execution  %@", [Nsthread CurrentThread]);  });

3. Using Nstimer

[Nstimer scheduledtimerwithtimeinterval:2.0  target:self selector: @selector (Test) Userinfo:nil repeats: NO]; -(void) test{    NSLog (@ "----BEGIN-----");    [Nsthread sleepfortimeinterval: 3 // it will snap the thread    NSLog (@ "----End-----");}

4, [Nsthread Sleepfortimeinterval:3] will be stuck thread, not

-(void) dely1{  //  1, delay execution do not use Sleepfortimeinterval (it will snap thread)  NSLog (@ "  ----BEGIN-----");  [Nsthread sleepfortimeinterval: 3 ];  NSLog (@ "----End-----");}

Commonly used time-lapse execution

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.