虽然之前看过这四个名词,但是,有时候,还是会混淆他们,在次重新解释下:
-(void)syncTest:(NSNumber *)number { NSLog(@"syncTest start"); int m = [number integerValue]; dispatch_sync(_queue, ^{ NSLog(@"我是花花1---%d",m); NSLog(@"我是花花2---%d",m); NSLog(@"我是花花3---%d",m); NSLog(@"我是花花4---%d",m); NSLog(@"我是花花5---%d",m); }); NSLog(@"syncTest end"); }
-(void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; DataBase *db = [[DataBase alloc] init]; [db performSelectorInBackground:@selector(syncTest:) withObject:@(1)]; [db performSelectorInBackground:@selector(syncTest:) withObject:@(2)]; [db performSelectorInBackground:@selector(syncTest:) withObject:@(3)]; [db performSelectorInBackground:@selector(syncTest:) withObject:@(4)]; }
以上面的代码块为例:
如果执行顺序为:
start >>花花>>end,则为同步
start>>end>>花花,则为异步 同步 异步只跟是否立即返回有关系,跟代码块里面的执行顺序没有关系
如果执行顺序为:
2015-03-02 15:39:56.260 ARCMemoryTest[470:154875] syncTest start 2015-03-02 15:39:56.261 ARCMemoryTest[470:154876] syncTest start 2015-03-02 15:39:56.261 ARCMemoryTest[470:154877] syncTest start 2015-03-02 15:39:56.262 ARCMemoryTest[470:154878] syncTest start 2015-03-02 15:39:56.263 ARCMemoryTest[470:154875] 我是花花1---1 2015-03-02 15:39:56.303 ARCMemoryTest[470:154875] 我是花花2---1 2015-03-02 15:39:56.309 ARCMemoryTest[470:154875] 我是花花3---1 2015-03-02 15:39:56.311 ARCMemoryTest[470:154875] 我是花花4---1 2015-03-02 15:39:56.312 ARCMemoryTest[470:154875] 我是花花5---1 2015-03-02 15:39:56.314 ARCMemoryTest[470:154875] syncTest end 2015-03-02 15:39:56.317 ARCMemoryTest[470:154876] 我是花花1---2 2015-03-02 15:39:56.319 ARCMemoryTest[470:154876] 我是花花2---2 2015-03-02 15:39:56.328 ARCMemoryTest[470:154876] 我是花花3---2 2015-03-02 15:39:56.329 ARCMemoryTest[470:154876] 我是花花4---2 2015-03-02 15:39:56.332 ARCMemoryTest[470:154876] 我是花花5---2 2015-03-02 15:39:56.333 ARCMemoryTest[470:154877] 我是花花1---3 2015-03-02 15:39:56.333 ARCMemoryTest[470:154876] syncTest end 2015-03-02 15:39:56.334 ARCMemoryTest[470:154877] 我是花花2---3 2015-03-02 15:39:56.336 ARCMemoryTest[470:154877] 我是花花3---3 2015-03-02 15:39:56.337 ARCMemoryTest[470:154877] 我是花花4---3 2015-03-02 15:39:56.338 ARCMemoryTest[470:154877] 我是花花5---3 2015-03-02 15:39:56.340 ARCMemoryTest[470:154877] syncTest end 2015-03-02 15:39:56.342 ARCMemoryTest[470:154878] 我是花花1---4 2015-03-02 15:39:56.344 ARCMemoryTest[470:154878] 我是花花2---4 2015-03-02 15:39:56.345 ARCMemoryTest[470:154878] 我是花花3---4 2015-03-02 15:39:56.347 ARCMemoryTest[470:154878] 我是花花4---4 2015-03-02 15:39:56.348 ARCMemoryTest[470:154878] 我是花花5---4 2015-03-02 15:39:56.349 ARCMemoryTest[470:154878] syncTest end
如上述所述,block块中的代码按顺序执行,称为串行。一条河在流,顺序执行
2015-03-02 15:42:03.860 ARCMemoryTest[476:155325] syncTest start 2015-03-02 15:42:03.860 ARCMemoryTest[476:155327] syncTest start 2015-03-02 15:42:03.860 ARCMemoryTest[476:155326] syncTest start 2015-03-02 15:42:03.860 ARCMemoryTest[476:155328] syncTest start 2015-03-02 15:42:03.871 ARCMemoryTest[476:155325] 我是花花1---1 2015-03-02 15:42:03.874 ARCMemoryTest[476:155327] 我是花花1---3 2015-03-02 15:42:03.875 ARCMemoryTest[476:155326] 我是花花1---2 2015-03-02 15:42:03.877 ARCMemoryTest[476:155328] 我是花花1---4 2015-03-02 15:42:03.879 ARCMemoryTest[476:155325] 我是花花2---1 2015-03-02 15:42:03.881 ARCMemoryTest[476:155327] 我是花花2---3 2015-03-02 15:42:03.884 ARCMemoryTest[476:155326] 我是花花2---2 2015-03-02 15:42:03.886 ARCMemoryTest[476:155328] 我是花花2---4 2015-03-02 15:42:03.887 ARCMemoryTest[476:155325] 我是花花3---1 2015-03-02 15:42:03.887 ARCMemoryTest[476:155327] 我是花花3---3 2015-03-02 15:42:03.888 ARCMemoryTest[476:155326] 我是花花3---2 2015-03-02 15:42:03.889 ARCMemoryTest[476:155328] 我是花花3---4 2015-03-02 15:42:03.890 ARCMemoryTest[476:155325] 我是花花4---1 2015-03-02 15:42:03.890 ARCMemoryTest[476:155327] 我是花花4---3 2015-03-02 15:42:03.891 ARCMemoryTest[476:155326] 我是花花4---2 2015-03-02 15:42:03.892 ARCMemoryTest[476:155328] 我是花花4---4 2015-03-02 15:42:03.894 ARCMemoryTest[476:155325] 我是花花5---1 2015-03-02 15:42:03.894 ARCMemoryTest[476:155327] 我是花花5---3 2015-03-02 15:42:03.895 ARCMemoryTest[476:155326] 我是花花5---2 2015-03-02 15:42:03.896 ARCMemoryTest[476:155328] 我是花花5---4 2015-03-02 15:42:03.897 ARCMemoryTest[476:155325] syncTest end 2015-03-02 15:42:03.897 ARCMemoryTest[476:155327] syncTest end 2015-03-02 15:42:03.899 ARCMemoryTest[476:155326] syncTest end 2015-03-02 15:42:03.900 ARCMemoryTest[476:155328] syncTest end