dispatch_after与performSelector延迟区别

 1.在主线程中

-(void)operationUse{
      dispatch_queue_t serialQueue = dispatch_queue_create("12312312", DISPATCH_QUEUE_CONCURRENT);

      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"----4-----%@", [NSThread currentThread]);
        });
    
     [self performSelector:@selector(test1) withObject:nil afterDelay:0];
     [self performSelector:@selector(test2)];
   
}

-(void)test1{
     NSLog(@"----1-----%@", [NSThread currentThread]);
}
-(void)test2{
     NSLog(@"----2-----%@", [NSThread currentThread]);
}

2018-04-07 17:02:22.472123+0800 COCOCOCO[4207:170568] ----2-----<NSThread: 0x60000006b800>{number = 1, name = main}

2018-04-07 17:02:22.472923+0800 COCOCOCO[4207:170568] ----1-----<NSThread: 0x60000006b800>{number = 1, name = main}

2018-04-07 17:02:24.672263+0800 COCOCOCO[4207:170568] ----4-----<NSThread: 0x60000006b800>{number = 1, name = main}

这里延迟时间为0,但是如果当前线程有其他任务,优先让步给其他任务,执行等级相当于在主线程dispatch_async,把它的任务添加到队列末尾。

2.dispatch_after延迟时间与perform延迟时间一致谁先执行?

-(void)operationUse{
      dispatch_queue_t serialQueue = dispatch_queue_create("12312312", DISPATCH_QUEUE_CONCURRENT);

      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"----4-----%@", [NSThread currentThread]);
        });
    
     [self performSelector:@selector(test1) withObject:nil afterDelay:2.0];
    // [self performSelector:@selector(test2)];
   
}

-(void)test1{
     NSLog(@"----1-----%@", [NSThread currentThread]);
}
-(void)test2{
     NSLog(@"----2-----%@", [NSThread currentThread]);
}
打印:2018-04-07 17:08:03.757620+0800 COCOCOCO[4266:174055] ----1-----<NSThread: 0x600000075a00>{number = 1, name = main}
2018-04-07 17:08:03.954315+0800 COCOCOCO[4266:174055] ----4-----<NSThread: 0x600000075a00>{number = 1, name = main}

看到虽然dispatch_after在perform前边,但是perform优先执行,因为dispatch_after是延迟提交,不是延迟运行

3.在子线程中两者的区别

-(void)operationUse{
      dispatch_queue_t serialQueue = dispatch_queue_create("12312312", DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(serialQueue, ^{
        NSLog(@"----3-----%@", [NSThread currentThread]);
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"----4-----%@", [NSThread currentThread]);
        });
        [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(test3) userInfo:nil repeats:NO];
        
        [self performSelector:@selector(test1) withObject:nil afterDelay:2.0];
        
    });
     [self performSelector:@selector(test2)];
   
}

-(void)test1{
     NSLog(@"----1-----%@", [NSThread currentThread]);
}
-(void)test2{
     NSLog(@"----2-----%@", [NSThread currentThread]);
}
-(void)test3{
     NSLog(@"----5-----%@", [NSThread currentThread]);
}
打印:2018-04-07 17:18:54.389757+0800 COCOCOCO[4398:181354] ----2-----<NSThread: 0x6000000799c0>{number = 1, name = main}
2018-04-07 17:18:54.389913+0800 COCOCOCO[4398:181576] ----3-----<NSThread: 0x604000465b80>{number = 3, name = (null)}
2018-04-07 17:18:56.392155+0800 COCOCOCO[4398:181354] ----4-----<NSThread: 0x6000000799c0>{number = 1, name = main}
可以看到perform与NSTimer没有执行,
只有主线程会在创建的时候默认自动运行一个runloop,并且有timer,普通的子线程是没有这些的。这样在子线程中被调用的时候,我们的代码中的延时调用的代码就会一直等待timer的调度,但是实际上在子线程中又没有这样的timer,这样我们的代码就永远不会被调到。因此,perform使用时需要注意环境!

4.如何在子线程调用perform和NStimer?

-(void)operationUse{
      dispatch_queue_t serialQueue = dispatch_queue_create("12312312", DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(serialQueue, ^{
        @autoreleasepool {
            [NSThread currentThread].name = @"自定义线程名字";
            NSRunLoop * runloop = [NSRunLoop currentRunLoop];
            [runloop addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
             [self performSelector:@selector(test1) withObject:nil afterDelay:2.0];
            
            [runloop run];
            
            [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(test3) userInfo:nil repeats:NO];
        }
       
        
    });
     [self performSelector:@selector(test2)];
   
}
结果:2018-04-07 17:57:23.669567+0800 COCOCOCO[4778:202838] ----2-----<NSThread: 0x60400006a140>{number = 1, name = main}
2018-04-07 17:57:25.675078+0800 COCOCOCO[4778:202894] ----1-----<NSThread: 0x604000465340>{number = 3, name = 自定义线程名字}
perform执行了,NSTimer没有加入runloop中未执行

猜你喜欢

转载自blog.csdn.net/HuberCui/article/details/79842992