• 多线程的创建方法


    - (void)viewDidLoad {
        [super viewDidLoad];
       

        //第一种开启新的线程调用 mutableTheard
        NSThread * t = [[NSThread alloc]initWithTarget:self selector:@selector(mutableTheard) object:nil];
        [t start];
        
        
        //第二种开启新的线程调用 mutableTheard
        [NSThread detachNewThreadSelector:@selector(mutableTheard) toTarget:self withObject:nil];
        
        //第三种开新的线程 mutableTheard
        [self performSelectorInBackground:@selector(mutableTheard) withObject:self];
        
        //第四种开新的线程
        NSOperationQueue *operationQueue = [[NSOperationQueue alloc]init];
        [operationQueue addOperationWithBlock:^{
            for (int i = 1; i <100; i++) {
                NSLog(@"---多线程");
            }
        }];

        
        //第五种开新的线程
        NSOperationQueue * Queue = [[NSOperationQueue alloc]init];
        //设置线程执行的并发数。
        Queue.maxConcurrentOperationCount = 2;
        //创建一个线程惭怍对象
        NSInvocationOperation * operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(mutableTheard) object:nil];
       //创建一个线程惭怍对象
        NSInvocationOperation *operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(mutableTheard) object:nil];
        //把线程添加到队列中
        [operationQueue  addOperation:operation1];
        [operationQueue addOperation:operation2];
        
        
        
        //第六种开新的线程GCD
        dispatch_queue_t queue = dispatch_queue_create("test", NULL);
        dispatch_async(queue, ^{
            NSLog(@"我在多线程");
        });
        
        //可以使用这个方法可以回到主线程
        dispatch_sync(dispatch_get_main_queue(), ^{
            BOOL isMain = [NSThread isMainThread];
            if (isMain == YES) {
           NSLog(@"回到主线程了");
            }
            
        });
        
        
        //用过这种方法,还是同步在当前线程上
        dispatch_sync(queue, ^{
            ///当前线程
        });
        
        
    }


    -(void)mutableTheard
    {
    ///创建自动释放池
        @autoreleasepool {
         
            
         [self  performSelectorOnMainThread:@selector(mainTheard) withObject:nil waitUntilDone:YES];// 回到主线程
            

        }
     }
     

     
  • 相关阅读:
    简单的BMCP位图图片压缩算法
    163相册验证码图片的识别手记之二 识别
    认父亲的DbParameter!!
    文件同步精灵(初版)
    163相册验证码图片的识别手记之一 去除干扰
    C#中WebService里的回车符"\r"丢失问题
    PHP 杂谈《重构改善既有代码的设计》之二 对象之间搬移特性
    PHP5计划任务离线功能的原理
    (转)程序员疫苗:代码注入
    window7环境,不安装Oracle,使用PL/SQL Developer结合oracle精简客户端,管理Oracle数据库
  • 原文地址:https://www.cnblogs.com/canghaixiaoyuer/p/4561434.html
Copyright © 2020-2023  润新知