线程 NSThread

2017-05-18  本文已影响0人  小的小碰撞

NSThread

NSThread *myThread = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"jack"];
    
 [myThread start]; // 开始
[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
[self performSelectorInBackground:@selector(run:) withObject:nil];
 [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
- (void)start;

● 强制停止线程

● 互斥锁使用格式
@synchronized(锁对象)
{ // 需要锁定的代码 }
注意:锁定1份代码只用1把锁,用多把锁是无效的
● 互斥锁的优缺点
● 优点:能有效防止因多线程抢夺资源造成的数据安全问题
● 缺点:需要消耗大量的CPU资源
● 互斥锁的使用前提:多条线程抢夺同一块资源

上一篇 下一篇

猜你喜欢

热点阅读