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