NSThread创建方式

三种创建方式:

1.通过alloc init 方式创建线程并执行

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

[thread1 setName:@"Namethread1"];//获取线程name [NSThread currentThread].name;可用于判断线程问题

[thread1 setThreadPriority:0.5];//线程优先级,越大优先级越高

[thread1 start];

- (void)runThread{//子线程

for (int i = 0,i<10,i++){

NSLog(@"%d",i);

sleep(1);

if(i=10){//如果i为10的话,回到主线程

[self performSelectorOnMainThread:@selector(runMainThread) withObject:nil waitUntilDone:YES];

}

}

}

- (void)runMainThread{

NSLog(@"回到主线程");

}

2.通过 detachNewThreadSelector 创建线程并执行

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

3.通过 performSelectorInBackground 创建线程并执行

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

加锁:

1.@synchronized(self){

}

2.NSCondiction *condition = [[NSCondiction alloc]init];

[condition lock];

[condition unlock];

3.nslock

 

转载于:https://www.cnblogs.com/liuting-1204/p/6573677.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值