IOS 学习 多线程

1 不要同时开太多线程 1~3条

2线程概念
主线程 UI线程 不要把耗时操作放在主线程
子线程

一 NSThread
1 创建和启动线程的三种方式
1)先创建 后启动
// 创建
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(download:) object:nil];

// 启动
[thread start];

2) 创建完自动启动
[NSTgread detachNewThreadSelector:@selector(download:) toTarget:self withObject:nil];

3)隐式创建(自动启动)
[self performSelectorBackground:@selector(download:) withObject:nil];

2 创建方法
获得当前线程
+(NSThread *)currentThread;

2) 获得主线程
+(NSThread *)mainThread;

3) 睡眠(暂停) 线程
+(void)sleepUntilData:(NSDate *)date;
+(void)sleepForTimeInterval:(NSTimeInterval)ti;

4) 设置线程的名字
-(void) setName:(NSString *)n;
-(NSString *)namel

二 线程同步
1 实质 为了防止多个线程抢夺同一个资源造成的数据安全问题

2 实现 给代码加一个互斥锁
@synchronized(self){
//被锁住的代码
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值