多线程,进程

来源:互联网 发布:上海一姐张弦淘宝店 编辑:程序博客网 时间:2024/06/14 09:45

进程:程序在计算机的一次执行活动,一个程序就是一个进程,在IOS中一个APP就是一个进程

 线程:程序执行的最小单元.一个进程中至少有一个线程(主线程)

   线程中的注意几点

- (void)banZhuanPlus
{
1,线程中的autorelease对象不能释放,必须手动释放或添加自动释放池
2,子线程中刷新UI可能失败(在子线程中不要刷新UI)
    @autoreleasepool {
        for (int i = 0 ; i<1000; i++) {
            NSString *string = [NSString stringWithFormat:@"搬了%d块砖",i];
            NSLog(@"%@",string);
        }
    }
    
    
    在主线程中刷新UI
    [self performSelectorOnMainThread:@selector(refreshUI) withObject:nil waitUntilDone:YES];

}

- (void)refreshUI

{

    self.view.backgroundColor = [UIColor redColor];

}


- (IBAction)banzhuanPro:(id)sender
{
    //多线程的第一种方法(NSObject)创建线程
    //自动开辟后台线程
    //参数一:在后台线程中执行的方法
    //参数二:用于传递参数
    [self performSelectorInBackground:@selector(banZhuanPlus) withObject:nil];
    
}

- (IBAction)banzhuanWithNSThread:(id)sender

{

    多线程的第二种方法(NSThread)创建线程

    参数1,方法执行者

    参数2,在线程中执行的方法

    参数3,传递参数

    

    第一步,创建线程


    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(banZhuanPlus) object:nil];

        第二部,执行

    [thread start];

    [thread release];

}

- (IBAction)banzhuanWithNSOperationQueue:(id)sender
{
    NSOperation是操作单元,用来执行方法,是一个抽象的类,必须子类化或者使用系统创建好的子类,(NSInvocationOperation or NSBlockOperation)
    1,创建
    NSInvocationOperation *invocation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(banZhuanPlus) object:nil];
    2,在主线程中执行
    [invocation start];
    
    
    第一步:创建
    NSBlockOperation *block = [NSBlockOperation blockOperationWithBlock:^{
        [self banZhuanPlus];
    }];
    第二部:执行
    [block start];
    
    NSOperation是最小的操作单元,只能执行一次
    这个队列会自动帮咱们创建一个辅助线程
    这个队列只能够添加NSOperationQueue及子类对象
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    设置最大并行数量
    [queue setMaxConcurrentOperationCount:1];
    只有把操作单元添加到队列中,他就执行
    [queue addOperation:block];
    [queue addOperation:invocation];
    
    队列:先进先出.栈:先进后出
    队列里涉及到(串行)和并行
    串行:一次只能执行一次任务
    并行:一次可以执行多次任务
    
}
- (IBAction)banzhuanWithGCD:(id)sender
{
    //GCD:Grand Central Dispatch ,大中央调度,是苹果最推崇的多线程管理策略,通过队列去对多线程进行管理的
    
//    第一种队列:
    //主调队列,在主线程中执行,并且串行(一个操作一个)
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    NSLog(@"%@",mainQueue);
    
    //第二种
    //全局队列,在子线程中执行,并且并行(一次执行多个)
    //参数一,设置队列的优先级(high,default,low,background)
    //参数二,预留参数,未来适用
    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    NSLog(@"%@",globalQueue);
    //第三种队列
    //自定义队列,在子线程中执行,可以设置串行,并行......
    //参数一,区分队列的唯一标识,可选项,如果不写,NULL
//    如果写,必须规范例子"com.example.myqueue"
    //参数二,设置并行或串行的
    //并行:DISPATCH_QUEUE_CONCURRENT
    //串行:DISPATCH_QUEUE_SERIAL (or NULL)
    dispatch_queue_t customQueue = dispatch_queue_create("com.example.myqueue", DISPATCH_QUEUE_CONCURRENT);
    NSLog(@"%@",customQueue);
    
    //同步执行
    //参数一,指定队列,参数二,block,执行的操作
    dispatch_sync(globalQueue, ^{
        [self banZhuanPlus];
    });
    //异步执行(无需等待)
    dispatch_async(globalQueue, ^{
        [self banZhuanPlus];
    });
//延迟执行
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(<#delayInSeconds#> * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        code to be executed after a specified delay
//    });
    
}






0 0
原创粉丝点击