zoukankan      html  css  js  c++  java
  • 多线程 CGD快速迭代

     

    #import "ViewController.h"

     

    @interface ViewController ()

     

    @end

     

    @implementation ViewController

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

     

        [self moveFileWithGCD];

     }

     

    -(void)forDome

    {

    //同步

        for (NSInteger i = 0; i<10; i++) {

            NSLog(@"%zd---%@",i,[NSThread currentThread]);

        }

    }

     

    //开子线程和主线程一起完成遍历任务,任务的执行时并发的

    - (void)applyDemo

    {

        /*

         第一个参数:遍历的次数

         第二个参数:队列(并发队列)

         第三个参数:index 索引

         */

        dispatch_apply(10, dispatch_get_global_queue(0, 0), ^(size_t index) {

            NSLog(@"%zd---%@",index,[NSThread currentThread]);

        });

    }

     

     

    //使用for循环

    - (void)moveFile{

    //1.拿到文件路径

        NSString *form = @"/Users/liuzhenjie/Desktop/from";

    //2.获取目标文件路劲

        NSString *to = @"/Users/liuzhenjie/Desktop/to";

    //    3.得到目录下面的所有文件

        NSArray *subPaths = [[NSFileManager defaultManager] subpathsAtPath:form];

        NSLog(@"%@",subPaths);

    //4.遍历所有文件,然后执行剪切操作

        NSInteger count = subPaths.count;

        

        for (NSInteger i=0; i<count; i++) {

    //4.1拼接文件的全路径

    //        NSString *fullPath = [form stringByAppendingString:subPaths[i]];

            NSString *fullPath = [form stringByAppendingPathComponent:subPaths[i]];

            NSString *toFullPath = [to stringByAppendingPathComponent:subPaths[i]];

            NSLog(@"%@",fullPath);

            

    //        4.2执行剪切操作

            /**

             *  第一个参数:要剪切的文件在哪里

    //         第二个参数:文件应该被存到那个位置

             */

            [[NSFileManager defaultManager]moveItemAtPath:fullPath toPath:toFullPath error:nil];

            NSLog(@"%@---%@--%@",fullPath,toFullPath,[NSThread currentThread]);

        }

    }

     

    //使用GCD

    -(void)moveFileWithGCD

    {

        //1.拿到文件路径

        NSString *from = @"/Users/liuzhenjie/Desktop/from";

        //2.获得目标文件路径

        NSString *to = @"/Users/liuzhenjie/Desktop/to";

        //3.得到目录下面的所有文件

        NSArray *subPaths = [[NSFileManager defaultManager] subpathsAtPath:from];

        

        //4.遍历所有文件,然后执行剪切操作

        NSInteger count = subPaths.count;

       dispatch_apply(count, dispatch_get_global_queue(0, 0), ^(size_t i) {

           

           

           //4.1 拼接文件的全路径

           // NSString *fullPath = [from stringByAppendingString:subPaths[i]];

           //在拼接的时候会自动添加/

           NSString *fullPath = [from  stringByAppendingPathComponent:subPaths[i]];

           NSString *toFullPath = [to stringByAppendingPathComponent:subPaths[i]];

           

           //4.2 执行剪切操作

           /*

            第一个参数:要剪切的文件在哪里

            第二个参数:文件应该被存到哪个位置

            */

           [[NSFileManager defaultManager]moveItemAtPath:fullPath toPath:toFullPath error:nil];

           NSLog(@"%@---%@---%@",fullPath,toFullPath,[NSThread currentThread]);

           

       });

        

        

     

    }

     

     

     

     

     

     

     

    @end

  • 相关阅读:
    Linux 安装网络yum地址
    MYSQL登录错误:mysqladmin: connect to server at ‘localhost’ failed
    linux 中截取字符串
    screen 调到后台使用
    Yum 安装memcached 与缓存清空
    LAMP 环境搭建
    DELL--R420 CPU报警“CPU0000 cpu2 internal error (IERR)contact support”
    解决vim粘贴时格式混乱的问题
    DELL 管理软件安装
    windows 使用SVN命令
  • 原文地址:https://www.cnblogs.com/liuzhenjie/p/5203292.html
Copyright © 2011-2022 走看看