美文网首页
多线程编程之NSThread

多线程编程之NSThread

作者: 阿咿呀嘿 | 来源:发表于2015-04-05 18:03 被阅读105次

NSThread创建线程很简单,管理线程很困难

一、创建线程

1、创建线程对象,调用start方法执行线程

NSThread*thread = [[NSThread alloc] initWithTarget:self selector:@selector(testAction:) object:@"xxx"];

//设置线程名称

thread.name=@"testThread";

//设置线程优先级

[NSThread setThreadPriority:0.3];

//执行线程

[threadstart];

2、直接创建线程并执行

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

二、常用属性或方法

1、获取主线程

NSThread*mainThread = [NSThread mainThread];

2、判断是否是主线程

BOOL isMainThread = [mainThread isMainThread];

3、获取当前线程

NSThread*currentThread = [NSThread currentThread];

4、暂停当前线程

[NSThread sleepForTimeInterval:2.0];

三、NSObject的多线程方法

NSObject的多线程方法就是使用了NSThread,使用简单,轻量级,但是不能控制线程数量及执行顺序,而且涉及对象分配时不会自动调用@autoreleasepool自动释放池。

1、在后台线程执行任务

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

2、在主线程执行任务

[self performSelectorOnMainThread:@selector(testAction2:) withObject:nil waitUntilDone:NO];

3、在指定线程执行任务

[self performSelector:@selector(testAction2:) onThread:mainThread withObject:nil waitUntilDone:NO];

四、多线程安全-线程同步

在多线程编程中,多个线程可以共享同一个资源,如果多个线程同时访问这一资源,可能发生数据错乱问题,比如多个线程同时修改同一个变量。解决方法:使用互斥锁、使用原子或非原子属性、使用NSLock、使用信号量机制。

例子:开三个线程A、B、C售票

- (void)test

{

NSLog(@"Thread Safe Testing!");

self.ticketNum=10;

//售票线程A

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

threadA.name=@"线程A";

//售票线程B

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

threadB.name=@"线程B";

//售票线程C

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

threadC.name=@"线程C";

[threadA start];

[threadB start];

[threadC start];

}

1、使用互斥锁@synchronized

- (void)sell

{

while(1)

{

@synchronized(self)

{

NSInteger left =self.ticketNum;

if(left >0)

{

[NSThread sleepForTimeInterval:2.0];

self.ticketNum= left -1;

NSLog(@"%@卖出1张票,剩余票数%li", [NSThread currentThread].name, (long)self.ticketNum);

}

else

{

NSLog(@"售票结束!");

[NSThread exit];

}

}

}

}

2、使用NSLock

self.myLock= [[NSLock alloc] init];

- (void)sell

{

while(1)

{

[self.myLock lock];

NSIntegerleft =self.ticketNum;

if(left >0)

{

[NSThread sleepForTimeInterval:2.0];

self.ticketNum= left -1;

NSLog(@"%@卖出1张票,剩余票数%li", [NSThread currentThread].name, (long)self.ticketNum);

}

else

{

NSLog(@"售票结束!");

[NSThread exit];

}

[self.myLock unlock];

}

}

相关文章

  • iOS多线程相关面试题

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

  • 多线程之--NSOperation

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

  • iOS多线程之--NSThread

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

  • OC_NSThread

    原文链接:iOS多线程--彻底学会多线程之『pthread、NSThread』 **NSThread **是苹果官...

  • 7.2 多线程-NSThreed

    多线程-NSThreed.png NSThread案列 NSThread线程之间的通讯 NSThread线程的优先...

  • iOS四种多线程的基本使用

    1.pthread 实现多线程操作(不常使用) 2.NSThread实现多线程 3.多线程之GCD讲解 模拟两个异...

  • 多线程之NSThread

    一、介绍NSThread用来创建多线程、监视线程的运行时情况等,线程管理需要程序员自己手动操作。 二、API 注意...

  • 多线程之NSThread

    什么是NSThreadNSThread是基于线程使用,轻量级的多线程编程方法(相对GCD和NSOperation)...

  • 多线程之—NSThread

    是啦,面试的时候大概讲出在iOS开发中用到的几个多线程之后,面试官会继续详细咨询一番,测试你是否有真本事,接下来,...

  • 多线程之NSThread

    NSThread 优点:轻量级,更直观的控制线程对象 缺点:需要自己管理线程的生命周期,线程同步,线程枷锁。这会导...

网友评论

      本文标题:多线程编程之NSThread

      本文链接:https://www.haomeiwen.com/subject/gonaxttx.html