GCD基本使用-02-GCD

  1 //
  2 //  ViewController.m
  3 //  02-GCD基本使用
  4 //
  5 //  Created by mac on 16/4/21.
  6 //  Copyright © 2016年 mac. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 
 11 @interface ViewController ()
 12 
 13 @end
 14 
 15 @implementation ViewController
 16 
 17 - (void)viewDidLoad {
 18     [super viewDidLoad];
 19 }
 20 
 21 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
 22     
 23     //多线程正常开启
 24     [self asyncConcurrentQueue]; //1. 异步并发
 25     [self asyncSerialQueue]; //2. 异步串行
 26     
 27     //结果都是-主线程在执行
 28     [self syncConcurrentQueue]; //3. 同步并发
 29     [self syncSerialQueue];//4. 同步串行
 30     [self mainQueue]; //5. 主队列
 31     
 32     //死锁
 33     [self syncMainQueue]; //6. 同步主线程
 34 }
 35 
 36 /**
 37  *  async -- 主队列(很常用)
 38  *  线程之间通信比较常使用
 39  */
 40 - (void)mainQueue {
 41     
 42     //1. 主队列(添加到主队列中的任务,都会自动放到主线程中执行)
 43     dispatch_queue_t queue = dispatch_get_main_queue();
 44     
 45     //2.添加任务到主队列中异步执行
 46     dispatch_async(queue, ^{
 47         
 48         NSLog(@"1111==%@", [NSThread currentThread]);
 49     });
 50     dispatch_async(queue, ^{
 51         
 52         NSLog(@"2222==%@", [NSThread currentThread]);
 53     });
 54     dispatch_async(queue, ^{
 55         
 56         NSLog(@"3333==%@", [NSThread currentThread]);
 57     });
 58     dispatch_async(queue, ^{
 59         
 60         NSLog(@"4444==%@", [NSThread currentThread]);
 61     });
 62     dispatch_async(queue, ^{
 63         
 64         NSLog(@"5555==%@", [NSThread currentThread]);
 65     });
 66 
 67 }
 68 /**
 69  *   syncMain 主队列同步(不能用)
 70  */
 71 - (void)syncMainQueue {
 72     
 73     NSLog(@"syncMain=====");
 74     
 75     //1. 主队列(添加到主队列中的任务,都会自动放到主线程中执行)
 76     dispatch_queue_t queue = dispatch_get_main_queue();
 77     
 78     //2.添加任务到主队列中同步执行
 79     dispatch_sync(queue, ^{
 80         
 81         //等待中,产生死锁
 82         NSLog(@"1111==%@", [NSThread currentThread]);
 83     });
 84     dispatch_sync(queue, ^{
 85         
 86         NSLog(@"2222==%@", [NSThread currentThread]);
 87     });
 88     dispatch_sync(queue, ^{
 89         
 90         NSLog(@"3333==%@", [NSThread currentThread]);
 91     });
 92     dispatch_sync(queue, ^{
 93         
 94         NSLog(@"4444==%@", [NSThread currentThread]);
 95     });
 96     dispatch_sync(queue, ^{
 97         
 98         NSLog(@"5555==%@", [NSThread currentThread]);
 99     });
100     
101     //互相等待
102         NSLog(@"========syncMain=====");
103 }
104 
105 
106 /**
107  *  async--并发队列(最常用)
108  *  会创建线程:同时开多条
109  *  任务的执行方式:并发执行
110  */
111 - (void)asyncConcurrentQueue {
112     
113     //获取并发的全局队列
114     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
115     
116     //将 任务 添加到 全局队列 中去 异步执行
117     dispatch_async(queue, ^{
118         
119         NSLog(@"1111==%@", [NSThread currentThread]);
120     });
121     dispatch_async(queue, ^{
122         
123         NSLog(@"2222==%@", [NSThread currentThread]);
124     });
125     dispatch_async(queue, ^{
126         
127         NSLog(@"3333==%@", [NSThread currentThread]);
128     });
129     dispatch_async(queue, ^{
130         
131         NSLog(@"4444==%@", [NSThread currentThread]);
132     });
133     dispatch_async(queue, ^{
134         
135         NSLog(@"5555==%@", [NSThread currentThread]);
136     });
137 }
138 
139 /**
140  *  async--串行队列(有时候用)
141  *  会创建线程:只开一条线程
142  *  任务的执行方式:(顺序执行)
143  */
144 - (void)asyncSerialQueue {
145     
146     //1. 创建一个串行队列
147     dispatch_queue_t queue = dispatch_queue_create("随便写", NULL);
148     
149     //1. 将任务 添加到 串行队列中 去异步执行
150     dispatch_async(queue, ^{
151         
152         NSLog(@"1111==%@", [NSThread currentThread]);
153     });
154     dispatch_async(queue, ^{
155         
156         NSLog(@"2222==%@", [NSThread currentThread]);
157     });
158     dispatch_async(queue, ^{
159         
160         NSLog(@"3333==%@", [NSThread currentThread]);
161     });
162     dispatch_async(queue, ^{
163         
164         NSLog(@"4444==%@", [NSThread currentThread]);
165     });
166     dispatch_async(queue, ^{
167         
168         NSLog(@"5555==%@", [NSThread currentThread]);
169     });
170 }
171 
172 /**
173  *  sync--并发队列
174  *  不会创建线程:不开新线程,矛盾了
175  *  任务执行方式:串行执行(一个任务执行完,再执行下一个任务)
176  */
177 - (void)syncConcurrentQueue {
178     
179     //获取并发的全局队列
180     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
181     
182     //将 任务 添加到全局队列中去 同步执行
183     dispatch_sync(queue, ^{
184         
185         NSLog(@"1111==%@", [NSThread currentThread]);
186     });
187     dispatch_sync(queue, ^{
188         
189         NSLog(@"2222==%@", [NSThread currentThread]);
190     });
191     dispatch_sync(queue, ^{
192         
193         NSLog(@"3333==%@", [NSThread currentThread]);
194     });
195     dispatch_sync(queue, ^{
196         
197         NSLog(@"4444==%@", [NSThread currentThread]);
198     });
199     dispatch_sync(queue, ^{
200         
201         NSLog(@"5555==%@", [NSThread currentThread]);
202     });
203 }
204 
205 /**
206  *  sync--串行队列(不怎么用)
207  *  不会创建线程:不开新线程
208  *  任务执行方式:串行执行(一个任务执行完,再执行下一个任务)
209  */
210 - (void)syncSerialQueue {
211     
212     //1. 创建一个串行队列
213     dispatch_queue_t queue = dispatch_queue_create("随便写", NULL);
214     
215     //将 任务 添加到串行队列中去 同步执行
216     dispatch_sync(queue, ^{
217         
218         NSLog(@"1111==%@", [NSThread currentThread]);
219     });
220     dispatch_sync(queue, ^{
221         
222         NSLog(@"2222==%@", [NSThread currentThread]);
223     });
224     dispatch_sync(queue, ^{
225         
226         NSLog(@"3333==%@", [NSThread currentThread]);
227     });
228     dispatch_sync(queue, ^{
229         
230         NSLog(@"4444==%@", [NSThread currentThread]);
231     });
232     dispatch_sync(queue, ^{
233         
234         NSLog(@"5555==%@", [NSThread currentThread]);
235     });
236 }
237 
238 @end
    //非ARC --需要释放创建的队列
//    dispatch_release(queue);
    
    /**
     *  Foundation:OC
     *  Core Foundation:C
     *  Foundation和Core Foundation框架的数据类型可以互相转换
     */
    NSString *str = @"1234";
    CFStringRef str2 = (__bridge CFStringRef)str;
    NSString *str3 = (__bridge NSString *)str2;
    
    NSLog(@"%@=%@=%@", str, str2, str3);

桥接字符串

时光见证了成长,还很无知,我想一点点幼稚转为有知!
原文地址:https://www.cnblogs.com/foreveriOS/p/5415812.html