美文网首页
iOS——多线程pthread 和 NSThread

iOS——多线程pthread 和 NSThread

作者: 艳晓 | 来源:发表于2017-03-29 15:55 被阅读60次

iOS中的多线程方案分为4种,从最早的pthread到基于GCD的NSOperation.

1、pthread:(C语言,非常古老的一种多线程方法)开发中用的比较少
2、NSThread (OC语言,开发中用的也比较少,主要用于调试程序)
3、GCD (C语言,苹果进行了封装,开发中用的比较多)
4、NSOperation (基于GCD,开发中用的非常多)
其中3和4系统会自动进行内存管理。

一、pthread

pthread是一套纯用C语言的API,需要程序员自己管理生命周期,基本很少使用。

需要导入头文件#import<pthread/phtread.h>
<pre>
// 耗时操作
void * execFunc (void *param) {
NSString *result = (__bridge NSString *)param;

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

for (int i=0; i<100* 100; i++) {
    NSLog(@"%d",i);
}

return NULL;

}
//1、创建一条线程
pthread_t pthreadId;//线程Id
NSString * param = @"Hello, world";
int result = pthread_create(&pthreadId, NULL, execFunc, (__bridge void*)(param));
if (result == 0) {
NSLog(@"线程创建成功");
}else{
NSLog(@"线程创建失败");
}
</pre>

二、NSThread

NSThread是基于线程使用,轻量级的多线程编程方法(相对GCD和NSOperation),一个NSThread对象代表一个线程,需要手动管理线程的生命周期,处理线程同步等问题。

2.1、NSThread线程创建

NSThread线程创建的三种方法
1、对象方法
<pre>
-(void)dynamicCreateThread
{
// 创建线程对象
NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(loadImageSource:) object:self.imageUrl];
// 线程开启
[thread start];
}</pre>

2、创建线程后自动启动线程
<code>[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];</code>
3、隐式创建并启动线程
<code>[self performSelectorInBackground:@selector(run) withObject:nil]; </code>
3.1、结束当前所在线程回归主线程,并在主线程执行方法,使用Object传值,完成线程间通信。
<code>[self performSelectorOnMainThread:@selector(refreshImageView:) withObject:image waitUntilDone:YES];</code>

2.2、线程状态

程序中的线程可能的状态演变

线程的在不同时期的状态
线程状态的控制方法
1、启动线程
<code>- (void)start; </code>// 进入就绪状态 -> 运行状态。当线程任务执行完毕,自动进入死亡状态

2、阻塞(暂停)线程
<code>+ (void)sleepUntilDate:(NSDate *)date;</code>
<code>+ (void)sleepForTimeInterval:(NSTimeInterval)time;</code>

3、强制停止线程
<code>+ (void)exit;</code>// 进入死亡状态
注意:**一旦线程停止(死亡)了,就不能再次开启任务

2.3 NSThread注意事项

<code>- (void) cancel;</code>
1、当我们需要中途停止线程时,我们不应该调用exit方法,而是调用cancel方法。因为,如果我们直接调用
exit方法的话,线程是直接退出,而没有机会去执行清理操作,可能会产生内存泄漏!
<pre>// 停止线程

  • (void) stopThread{
    NSLog(@"Cancelling the Thread");
    [self.myThread cancel];
    NSLog(@"Releasing the thread");
    self.myThread = nil;
    }</pre>

2、我们必须要清楚这么一个现象!
当线程在执行过程中,如果被sleepForTimeInterval后,线程将会被进入休眠。那么在它休眠期间又被cancel后,那么,事实上,线程在醒来后,任然会执行完它的操作。

NSThread其余常用操作
主线程相关方法
<code>+ (NSThread *)mainThread; // 获得主线程</code>
<code>- (BOOL)isMainThread; // 是否为主线程</code>
<code>+ (BOOL)isMainThread; // 是否为主线程</code>
获得当前线程
<code>NSThread *current = [NSThread currentThread];</code>
线程的调度优先级
<code>+ (double)threadPriority;</code>
<code>+ (BOOL)setThreadPriority:(double)p;</code>
<code>- (double)threadPriority;</code>
<code>- (BOOL)setThreadPriority:(double)p;</code>
复制代码
调度优先级的取值范围是0.0 ~ 1.0,默认0.5,值越大,优先级越高

相关文章

  • OC_NSThread

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

  • 多线程(2)——NSThread

    iOS中实现多线程的四种方案 pthread NSThread GCD NSOpreation Pthread:这...

  • iOS多线程.md

    2018-05-22 iOS多线程-概念iOS多线程:『pthread、NSThread』详尽总结 多线程-概念图...

  • iOS开发中的多线程

    iOS开发中的多线程 pthread,NSThread,NSOpertion,GCD的区别 同步和异步,并发和串行...

  • 底层原理:多线程

    iOS中常见多线程方案 NSThread 、 GCD 和 NSOperation 底层都是依赖于 pthread ...

  • iOS之多线程之一(NSThread、NSOperation)

    前言 iOS多线程有四种:pthread(最古老的),NSThread,NSOperation,GCD 一、进程和...

  • iOS多线程

    iOS中常用的多线程:pthread:C语言 NSThread:OC GCD:C NSOpreration:...

  • NSThread和NSOperation

    前言 Pthread,NSThread,GCD和NSOperation是iOS中多线程的四种实现方案。 一.进程和...

  • iOS多线程之pthread和NSThread

    iOS开发中,多线程相关的知识点主要包括pthread、NSThread、NSOperation和GCD,我们经常...

  • iOS 多线程(四)GCD

    一、GCD简介 iOS开发中多线程的API主要有pthread,NSThread,NSOperation和GC...

网友评论

      本文标题:iOS——多线程pthread 和 NSThread

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