zoukankan      html  css  js  c++  java
  • GCD多线程机制

      1 #import "ViewController.h"
      2 
      3 @interface ViewController ()
      4 
      5 @end
      6 
      7 @implementation ViewController
      8 
      9 - (void)viewDidLoad {
     10     [super viewDidLoad];
     11     //向主线程添加一个任务
     12     //同步添加任务
     13     //GCD 主线程 dispatch_get_main_queue()
     14     //NSOperationQueue 主线程 [NSOperationQueue mainQueue]
     15 //    dispatch_sync(dispatch_get_main_queue(), ^{
     16 //        NSLog(@"1");
     17 //    });
     18     
     19     //异步添加任务
     20     dispatch_async(dispatch_get_main_queue(), ^{
     21         NSLog(@"%@",[NSThread currentThread]);
     22     });
     23     
     24     
     25     
     26 }
     27 //串行队列
     28 - (IBAction)dispatchQueueSerial:(id)sender {
     29     //创建串行队列
     30     //指针
     31     //dispatch_queue_create(字符串指队列的标识,串行或并行)
     32     dispatch_queue_t queue = dispatch_queue_create("com.lanou3g.queue1",DISPATCH_QUEUE_SERIAL );
     33     //添加任务
     34     dispatch_async(queue, ^{
     35         for ( int i = 0; i <100; i++) {
     36             NSLog(@"1,%@",[NSThread currentThread]);
     37         }
     38     });
     39     dispatch_async(queue, ^{
     40         for (int i = 0; i < 100; i++) {
     41              NSLog(@"2,%@",[NSThread currentThread]);
     42         }
     43        
     44     });
     45     
     46    
     47     
     48     
     49 }
     50 //全局队列
     51 - (IBAction)Dispatch_global_queue:(id)sender {
     52    //苹果预置了全局队列,并行队列
     53     //dispatch_get_global_queue(队列优先级,占位0)
     54     dispatch_queue_t global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
     55     //添加任务
     56     dispatch_async(global, ^{
     57         NSLog(@"1,%@",[NSThread currentThread]);
     58     });
     59     dispatch_async(global, ^{
     60         NSLog(@"2,%@",[NSThread currentThread]);
     61     });
     62     
     63     
     64     
     65 }
     66 //并行队列和全局队列是一样的
     67 - (IBAction)dispatch_queue_concurrent:(id)sender {
     68     dispatch_queue_t conQueue = dispatch_queue_create ("com.lanou3g.concurrent1", DISPATCH_QUEUE_CONCURRENT);
     69     //添加任务
     70     dispatch_async(conQueue, ^{
     71         for (int i = 0; i < 100; i++) {
     72             NSLog(@"1,%@",[NSThread currentThread]);
     73 
     74         }
     75     });
     76     dispatch_async(conQueue, ^{
     77         for (int i = 0; i < 100; i++) {
     78             NSLog(@"2,%@",[NSThread currentThread]);
     79             
     80         }
     81     });
     82 }
     83 //延时执行
     84 - (IBAction)DISPATCH_AFTER:(id)sender {
     85     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
     86         NSLog(@"延时,%@",[NSThread currentThread]);
     87     });
     88     
     89     
     90 }
     91 //重复执行
     92 - (IBAction)ReceiveMemoryWarning:(id)sender {
     93     //记得天机size_t的参数
     94     size_t count = 10;
     95     dispatch_apply(count, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^(size_t i) {
     96         NSLog(@"执行的次数:%ld",i);
     97     });
     98 }
     99 //任务添加到队列后,等待其他任务执行后再执行
    100 
    101 - (IBAction)DISPATCH_GROUP_NOTIFY:(id)sender {
    102     //把线程队列添加到组中
    103     //dispatch_group_async()向组中添加队列
    104     //dispatch_group_notify()添加任务,会等待其他任务执行完毕
    105     //创建线程组
    106     dispatch_group_t group = dispatch_group_create();
    107     dispatch_queue_t conQueue = dispatch_queue_create("com.lanou", DISPATCH_QUEUE_CONCURRENT);
    108     //添加任务
    109     dispatch_group_async(group, conQueue, ^{
    110         for (int i = 0; i <100; i++) {
    111             NSLog(@"1,%@",[NSThread currentThread]);
    112 
    113         }
    114     });
    115     dispatch_group_async(group, conQueue, ^{
    116         for (int i = 0; i <100; i++) {
    117             NSLog(@"2,%@",[NSThread currentThread]);
    118             
    119         }
    120     });
    121     //监听其他任务
    122     dispatch_group_notify(group, conQueue, ^{
    123         NSLog(@"等待其他任务执行后,再执行");
    124     });
    125 }
    126 //当前barrier线程执行时,其他任务停止
    127 - (IBAction)dispatchbarrieradync:(id)sender {
    128     dispatch_queue_t conQueue = dispatch_queue_create("com.lanou.conQueue", DISPATCH_QUEUE_CONCURRENT);
    129     //添加任务
    130     dispatch_async(conQueue, ^{
    131         for (int i = 0; i < 100; i++) {
    132             NSLog(@"1,%@",[NSThread currentThread]);
    133         }
    134     });
    135     dispatch_async(conQueue, ^{
    136         for (int i = 0; i < 100; i++) {
    137             NSLog(@"2,%@",[NSThread currentThread]);
    138         }
    139     });
    140     //添加barrier任务
    141     dispatch_barrier_async(conQueue, ^{
    142         for (int i = 0; i < 100; i++) {
    143             NSLog(@"barrier,%@",[NSThread currentThread]);
    144         }
    145     });
    146     dispatch_async(conQueue, ^{
    147         for (int i = 0; i < 100; i++) {
    148             NSLog(@"4,%@",[NSThread currentThread]);
    149         }
    150     });
    151     
    152     
    153 }
    154 
    155 //
    156 - (IBAction)dispatchAsyncF:(id)sender {
    157     
    158     dispatch_async_f(dispatch_get_main_queue(), nil, myFunction);   
    159     
    160 }
    161 //定义函数
    162 void myFunction(){
    163     for (int i = 0 ; i <100; i++) {
    164         NSLog(@"1,%@",[NSThread currentThread]);
    165     }
    166 }
    167 
    168 - (void)didReceiveMemoryWarning {
    169     [super didReceiveMemoryWarning];
    170     // Dispose of any resources that can be recreated.
    171 }
    172 
    173 @end
  • 相关阅读:
    Docker配置mysql
    Docker中部署Redis
    Linux上docker的安装
    tomcat启动报错:To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
    导出excel表格不能用ajax
    linux安装jdk
    第三次实验报告:使用Packet Tracer分析TCP连接建立过程
    第二次实验报告:使用Packet Tracer分析应用层协议
    第一次实验报告:使用Packet Tracer分析HTTP数据包
    PYTHON——多线程:队列Queue数据结构
  • 原文地址:https://www.cnblogs.com/DevinSMR/p/5321206.html
Copyright © 2011-2022 走看看