总结:同步(无论是串行还是并行)----不又一次开辟子线程

异步(无论是串行还是并行)----开辟子线程

GCD:

dispatch queue

主线程的main queue

并行队列 global dispatch queue

串行队列serial queues
一般用于按顺序同步訪问

#pragma mark - 载入多线程

- (void) _loadMutil

{

//GCD基于C语言

//1.主对列:(串行队列)

dispatch_queue_t mainQueue=dispatch_get_main_queue();

//2.全局并行队列

);

//3.创建串行队列

dispatch_queue_t queueSerial=dispatch_queue_create("jrqueue1",DISPATCH_QUEUE_SERIAL);

//4.创建并行队列

dispatch_queue_t queueConcu=dispatch_queue_create("jrqueue2",DISPATCH_QUEUE_CONCURRENT);

//同步运行+串行队列

/*

dispatch_sync(queueSerial, ^{

[NSThread sleepForTimeInterval:2];

NSLog(@"同步串行队列1-----%@",[NSThread currentThread]);

});

dispatch_sync(queueSerial, ^{

[NSThread sleepForTimeInterval:2];

NSLog(@"同步串行队列2-----%@",[NSThread currentThread]);

});

*/

//同步运行+并行队列

/*

dispatch_sync(queueConcu, ^{

[NSThread sleepForTimeInterval:2];

NSLog(@"同步并行队列1-----%@",[NSThread currentThread]);

});

dispatch_sync(queueConcu, ^{

[NSThread sleepForTimeInterval:2];

NSLog(@"同步并行队列2-----%@",[NSThread currentThread]);

});

*/

//异步运行+串行队列-----开启一个子线程,且顺序运行

/*

dispatch_async(queueSerial, ^{

[NSThread sleepForTimeInterval:2];

NSLog(@"异步串行队列1-----%@",[NSThread currentThread]);

});

dispatch_async(queueSerial, ^{

[NSThread sleepForTimeInterval:2];

NSLog(@"异步串行队列2-----%@",[NSThread currentThread]);

});

dispatch_async(queueSerial, ^{

[NSThread sleepForTimeInterval:2];

NSLog(@"异步串行队列3-----%@",[NSThread currentThread]);

});

*/

//异步运行+并行队列----开启多个线程,且并发运行(无序)

/*

dispatch_async(queueConcu, ^{

[NSThread sleepForTimeInterval:2];

NSLog(@"异步并行队列1-----%@",[NSThread currentThread]);

});

dispatch_async(queueConcu, ^{

[NSThread sleepForTimeInterval:2];

NSLog(@"异步并行队列2-----%@",[NSThread currentThread]);

});

dispatch_async(queueConcu, ^{

[NSThread sleepForTimeInterval:2];

NSLog(@"异步并行队列3-----%@",[NSThread currentThread]);

});

*/

//主对列+同步运行-----死锁(将下面两个加入到主队列,等待前面的运行完毕(loadView。

loadData之类的),可是当运行到这一步时,形成死循环)

/*

dispatch_sync(mainQueue, ^{

[NSThread sleepForTimeInterval:2];

NSLog(@"同步主队列1-----%@",[NSThread currentThread]);

});

dispatch_sync(mainQueue, ^{

[NSThread sleepForTimeInterval:2];

NSLog(@"同步主队列2-----%@",[NSThread currentThread]);

});

*/

}

@end


将以上的方法,在viewDidLoad 中调用一下。看看效果怎样~

GCD网络多线程---同步运行,异步运行,串行队列,并行队列的更多相关文章

  1. GCD中的dispatch_sync、dispatch_sync 分别与串行、并行队列组合执行小实验

    平常开发中会经常用gcd做一下多线程任务,但一直没有对同步.异步任务在串行.并行队列的执行情况做个全面的认识,今天写了个demo跑了下,还是有些新发现的. 代码如下: - (void)touchesB ...

  2. iOS GCD, 同步,异步,串行队列,并行队列,dispatch_group

    同步,指代码在同一个线程运行 异步,代码在另一个线程运行 串行队列,提交到该队列的block会顺序执行 并行队列,提交到该队列的block会并发执行 如果想等某一队列中所有block都执行完了在执行一 ...

  3. 【原】iOS多线程之异步任务+并行队列情况与异步任务+串行队列(主队列)情况

    异步任务+并行队列 把异步任务放到并行队列进行执行,异步任务会在不同的线程中执行. /*异步执行+并行队列*/ - (IBAction)clickBasic1:(UIButton *)sender { ...

  4. iOS gcd 串行,并行,同步,异步代码研究

    参考文章: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #527eff } span.s1 { } http: ...

  5. java面试一日一题:再谈垃圾回收器中的串行、并行、并发

    问题:请讲下java中垃圾回收器的串行.并行.并发 分析:该问题主要考察在垃圾回收过程中垃圾回收线程和用户线程的关系 回答要点: 主要从以下几点去考虑, 1.串行.并行.并发的概念 2.如何考虑串行. ...

  6. ios--进程/多线程/同步任务/异步任务/串行队列/并行队列(对比分析)

    现在先说两个基本的概念,啥是进程,啥是线程,啥又是多线程;先把这两个总是给弄清再讲下面的 进程:正在进行的程序,我们就叫它进程. 线程:线程就是进程中的一个独立的执行路径.这句话怎么理解呢! 一个程序 ...

  7. iOS:GCD理解1(同步-异步、串行-并行)

    1.并行-异步(ST1与ST2抢占资源) 1-1).获取 并行(全局)队列 ,DISPATCH_QUEUE_PRIORITY_DEFAULT 为默认优先级. dispatch_queue_t queu ...

  8. iOS 多线程的简单理解(2) 队列 :串行 ,并行,MainQueue,GlobalQueue

    多线程队列是装载线程任务的队形结构.(系统以先进先出的方式调度队列中的任务执行 FIFO).在GCD中有两种队列: 串行队列.并发队列. 队列 :串行队列.并发队列,全局主对列,全局并发队列 2.1. ...

  9. 深入GCD(四):使用串行队列实现简单的预加载

    其主要思路是使用gcd创建串行队列,然后在此队列中先后执行两个任务:1.预加载一个viewController 2.将这个viewController推入代码如下:@implementation DW ...

随机推荐

  1. 很好的资源 for android

    //texttospeach http://examples.javacodegeeks.com/android/core/text-to-speech/android-text-to-speech- ...

  2. BZOJ3238: [Ahoi2013]差异(后缀数组)

    Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao Sample Output 54 解题思路: 看到lcp,想到了 ...

  3. [CSS] Build a Fluid Loading Animation in CSS

    In this lesson, we will create a fluid loading animation using Animations and Transformations in CSS ...

  4. 跨域请求发送不了cookie问题: AJAX跨域请求JS配置和服务器端配置

    1.ajax是同步方式 $.ajax({ type: "post", url:url, async:false, data:datatosend, dataType:"j ...

  5. Java网络编程之TCP、UDP

    Java网络编程之TCP.UDP 2014-11-25 15:23 513人阅读 评论(0) 收藏 举报 分类: java基础及多线程(28) 版权声明:本文为博主原创文章,未经博主允许不得转载.   ...

  6. JVM route

    http://www.linuxidc.com/Linux/2013-06/86446.htm

  7. Android SimpleAdapter

    1.MainActivity.java public class MainActivity extends Activity { private ListView listView; private ...

  8. POJ 3264 Balanced Lineup 线段树RMQ

    http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...

  9. 利用Eclipse+openJTAG调试led.axf文件

    转自calvinlee1984 Subject:利用Eclipse+openJTAG调试led.axf文件 Date:     3-Mar-2011 By:         Calvinlee1984 ...

  10. JVM学习:方法重载的优先级

    重载:方法名一致,参数长度或者类型不一致. 先放总结,下面为例子 参数具有继承.实现关系,优先考虑子类: 在不考虑对基本类型自动装拆箱(auto-boxing,auto-unboxing),以及可变长 ...