zoukankan      html  css  js  c++  java
  • 多线程总结

    - (void)download:(NSString *)url
    {
        NSLog(@"下载东西---%@---%@", url, [NSThread currentThread]);
    }

    /**
     * 创建线程的方式3种
     */
    - (void)createThread
    {
      1、  NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(download:) object:@"http://b.png"];
        thread.name = @"下载线程";
        [thread start]; // 启动线程(调用self的download方法),一旦调用start,就把该线程对象放入线程池
     
      2、 [NSThread detachNewThreadSelector:@selector(download:) toTarget:self withObject:@"http://a.jpg"];
     
      3、 [self performSelectorInBackground:@selector(download:) withObject:@"http://c.gif"];
    }
     
    二、常见方法
    1> 获取当前线程
    +  (NSThread *) currentThread;
    2> 获取主线程
    + (NSThread *)mainThread;
    3>睡眠(暂停)线程
    +(void)sleepUntilDate:(NSDate *)date;
    +  (void) sleepForTimeInterval:(NSTimeInterval)ti;
    4>设置线程的名字
    - (void)setName:(NSString *)n;
    -   (NSString *) name;
     
    三、线程同步
    1.实质:为了防止多个线程抢夺同一个资源造成的数据安全问题
    2.实现:给代码加一个互斥锁(同步锁)
    @synchronized(self) {
     
    }
     
    四、线程通信
     // 4.回到主线程,刷新UI界面(为了线程安全)
        [self performSelectorOnMainThread:@selector(downloadFinished:) withObject:image waitUntilDone:NO];
    //    [self performSelector:@selector(downloadFinished:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];
    //    [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
     
    五、GCD
    // dispatch_sync : 同步,不具备开启线程的能力
    // dispatch_async : 异步,具备开启线程的能力

    // 并发队列 :多个任务可以同时执行
    // 串行队列 :一个任务执行完后,再执行下一个任务

    // Foundation :  OC
    // Core Foundation : C语言
    // Foundation和Core Foundation框架的数据类型可以互相转换的

    //NSString *str = @"123"; // Foundation
    //CFStringRef str2 = (__bridge CFStringRef)str; // Core Foundation
    //NSString *str3 = (__bridge NSString *)str2;
    //    CFArrayRef ---- NSArray
    //    CFDictionaryRef ---- NSDictionary
    //    CFNumberRef ---- NSNumber

    // Core Foundation中手动创建的数据类型,都需要手动释放
    //    CFArrayRef array = CFArrayCreate(NULL, NULL, 10, NULL);
    //    CFRelease(array);
    //
    //
    //    CGPathRef path = CGPathCreateMutable();
    //    CGPathRetain(path);
    //
    //    CGPathRelease(path);
    //    CGPathRelease(path);
    /**
     凡是函数名中带有createcopy ew etain等字眼, 都应该在不需要使用这个数据的时候进行release
     GCD的数据类型在ARC环境下不需要再做release
     CF(Core Foundation)的数据类型在ARCMRC环境下都需要再做release
     */
        // 获得全局的并发队列
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // 1.创建一个串行队列
        dispatch_queue_t queue = dispatch_queue_create("cn.heima.queue", NULL);
     // 1.主队列(添加到主队列中的任务,都会自动放到主线程中去执行)
        dispatch_queue_t queue = dispatch_get_main_queue();
        
    知识点:
    // 需要设置按钮的image和backgroundImage,建议先把按钮类型改为custom,才能保证设置成功
    // 属性名不能以new开头
    // 只有在init开头的构造方法中,才允许对self进行赋值
     
     
    Xib的加载
      // 控制器默认会自动找对应的xib来创建view
        // 1.去掉Controller这个单词后的同名的xib:HMHomeView.xib
        // 2.完全同名的xib:HMHomeViewController.xib
        // 3.自己创建新的View
     
      
     
  • 相关阅读:
    小a和uim之大逃离(dp)
    c++stl应用入门
    tar: 从成员名中删除开头的“/”
    yii中rights安装
    python中operator.itemgetter
    python中时间和时区
    python --那些你应该知道的知识点
    rsync拉取远程文件
    django中时区设置
    django中添加用户
  • 原文地址:https://www.cnblogs.com/zhengyumin/p/5174112.html
Copyright © 2011-2022 走看看