[关闭]
@PEND 2020-04-13T14:50:13.000000Z 字数 1502 阅读 359

dispatch打印结果

代码面试题


以下代码打印结果分别是什么?

  1. NSLog(@"%@, 1", [NSThread currentThread]);
  2. dispatch_queue_t queue = dispatch_queue_create("com.test.queue", NULL);
  3. dispatch_sync(queue, ^{
  4. NSLog(@"%@, 2", [NSThread currentThread]);
  5. });
  6. NSLog(@"3");
  7. NSLog(@"------分割线------");
  8. NSLog(@"%@, 1", [NSThread currentThread]);
  9. dispatch_queue_t queue1 = dispatch_queue_create("com.test.queue", NULL);
  10. dispatch_async(queue1, ^{
  11. NSLog(@"%@, 2", [NSThread currentThread]);
  12. });
  13. NSLog(@"3");
  14. NSLog(@"------分割线------");
  15. NSLog(@"%@, 1", [NSThread currentThread]);
  16. dispatch_queue_t queue2 = dispatch_queue_create("com.test.queue", DISPATCH_QUEUE_CONCURRENT);
  17. dispatch_sync(queue2, ^{
  18. NSLog(@"%@, 2", [NSThread currentThread]);
  19. });
  20. NSLog(@"3");
  21. NSLog(@"------分割线------");
  22. NSLog(@"%@, 1", [NSThread currentThread]);
  23. dispatch_queue_t queue3 = dispatch_queue_create("com.test.queue", DISPATCH_QUEUE_CONCURRENT);
  24. dispatch_async(queue3, ^{
  25. for (int i = 0; i < 2; i++) {
  26. NSLog(@"block1 %@", [NSThread currentThread]);
  27. }
  28. });
  29. dispatch_async(queue3, ^{
  30. for (int i = 0; i < 2; i++) {
  31. NSLog(@"block2 %@", [NSThread currentThread]);
  32. }
  33. });
  34. NSLog(@"3");
  35. NSLog(@"------分割线------");
  36. NSLog(@"%@, 1", [NSThread currentThread]);
  37. dispatch_queue_t queue4 = dispatch_get_main_queue();
  38. dispatch_sync(queue4, ^{
  39. NSLog(@"%@, 2", [NSThread currentThread]);
  40. });
  41. NSLog(@"3");
  42. NSLog(@"------分割线------");
  43. NSLog(@"%@, 1", [NSThread currentThread]);
  44. dispatch_queue_t queue5 = dispatch_get_main_queue();
  45. dispatch_async(queue5, ^{
  46. for (int i = 0; i < 2; i++) {
  47. NSLog(@"block1 %@", [NSThread currentThread]);
  48. }
  49. });
  50. dispatch_async(queue5, ^{
  51. for (int i = 0; i < 2; i++) {
  52. NSLog(@"block2 %@", [NSThread currentThread]);
  53. }
  54. });
  55. NSLog(@"3");
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注