多线程知识点(一)

1. for循环是不耗时的,i/o操作耗时

2. [NSThread currentThread]获取当前线程number代表线程的编号,name线程的名称,如果number为1则代表为主线程

3.使用pthread开启新的线程

  /*

        参数1:线程的编号地址

        参数2:线程的属性

        参数3:开启的线程要执行的函数 void*  (*)  (void *)

        参数4:给要执行的函数传递的参数

     */

   

    pthread_t ID;

    //开启子线程,自动销毁

    int result = pthread_create(&ID, NULL, demo, NULL);

    //如果返回值为0,则代表成功,其他的代表的失败

    if (result == 0) {

        NSLog(@"开启线程成功");

    }else

    {

        NSLog(@"开启线程失败");

    }

   

}

void* (demo)(void *param)

{

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

    return NULL;

}

4. __bridge桥接 可以把c->oc语言的字符串

NSString *str = (__bridge NSString *)(param);

5.开启子线程的三种方式:

//方式1

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

//    [thread start];

   

   

    //方式2

//    [NSThread detachNewThreadSelector:@selector(demo) toTarget:self withObject:nil];

   

    //方式3

//    [self performSelectorInBackground:@selector(demo) withObject:nil];

6.线程的状态:新建,就绪,运行,阻塞,死亡

7.线程的属性:

thread.name = @"t1";名称

[thread setThreadPriority:0];优先级

[NSThread currentThread].stackSize/1024占用内存

8. //互斥锁 (单个的进行读写操作)

         锁对象: 1 必须继承与NSObject

         2:必须是全局的

@synchronized(self.obj) {加锁的内容}

9.使用UIScrollView的时候记得添加contentsize

self.sv.contentSize = img.size;

10.直接使用nsurl下载图片

NSURL *url = [NSURL URLWithString:@"http://b.hiphotos.baidu.com/image/pic/item/08f790529822720ea5d058ba7ccb0a46f21fab50.jpg"];

    //下载图片

    NSData *data = [NSData dataWithContentsOfURL:url];

    //NSData - >UIImage

    UIImage *img = [UIImage imageWithData:data];

11

1:字符串用copy

2:代理要用weak(防止循环引用self - >p(具有代理属性) - >delegate - > self 循环引用)

3:block用copy

原文地址:https://www.cnblogs.com/chaoyueME/p/5574819.html