iOS多線程之--NSThread



iOS多線程demogit

iOS多線程之--NSThreadgithub

iOS多線程之--GCD詳解面試

iOS多線程之--NSOperation安全

iOS多線程之--線程安全(線程鎖)bash

iOS多線程相關面試題多線程



NSThread是蘋果官方提供面向對象操做線程的技術,簡單方便,能夠直接操做線程對象,不過須要本身控制線程的生命週期,平時開發中使用的並很少。post

1. 建立線程

NSThread實例化對象有2中方式,一種是經過target的方式執行任務,一種是經過block的方式執行任務。另外還有2種隱式建立線程的方式。動畫

1.1 target方式實例化

- (void)threadInitTarget{
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(task1) object:nil];
    thread.name = @"thread1"; // 給線程取名
    thread.threadPriority = 0.3; // 設置線程優先級
    [thread start]; // 啓動線程
}

- (void)task1{
    NSLog(@"task1--%@",[NSThread currentThread]);
}

// ***************打印結果***************
2019-12-31 15:43:16.889148+0800 MultithreadingDemo[42576:4668160] task1--<NSThread: 0x600002b17140>{number = 7, name = thread1}
複製代碼

1.2 block方式實例化

- (void)threadInitBlock{
    NSThread *thread = [[NSThread alloc] initWithBlock:^{
       NSLog(@"task2--%@",[NSThread currentThread]);
    }];
    thread.name = @"thread2";
    [thread start];
}

// ***************打印結果***************
2019-12-31 15:43:21.130273+0800 MultithreadingDemo[42576:4668204] task2--<NSThread: 0x600002beacc0>{number = 8, name = thread2}
複製代碼

1.3 target方式隱式建立線程

- (void)implicitThreadTarget{
    [NSThread detachNewThreadSelector:@selector(task3) toTarget:self withObject:nil];
}

- (void)task3{
    NSLog(@"task3--%@",[NSThread currentThread]);
}

// ***************打印結果***************
2019-12-31 16:24:31.760099+0800 MultithreadingDemo[55958:4841154] task3--<NSThread: 0x60000081dcc0>{number = 7, name = (null)}
複製代碼

1.4 block方式隱式建立線程

- (void)implicitThreadBlock{
    [NSThread detachNewThreadWithBlock:^{
        NSLog(@"task4--%@",[NSThread currentThread]);
    }];
}

// ***************打印結果***************
2019-12-31 16:24:34.696310+0800 MultithreadingDemo[55958:4841190] task4--<NSThread: 0x6000008fa7c0>{number = 8, name = (null)}
複製代碼

2. NSThread 經常使用屬性介紹

2.1 實例屬性

@property (nullable, copy) NSString *name;
複製代碼

給線程取個名字,只是方便調試時知道是哪一個線程,沒有其它實際用途。ui


@property double threadPriority;
複製代碼

線程優先級,取值範圍0.0-1.0,1.0表示最高優先級,默認是0.5.spa


@property NSQualityOfService qualityOfService;
複製代碼

這是iOS8以後新提供的設置優先級的方式。是一個枚舉類型:

  • NSQualityOfServiceUserInteractive:與用戶交互的任務,這些任務一般跟UI級別的刷新相關,好比動畫,這些任務須要在一瞬間完成。
  • NSQualityOfServiceUserInitiated:由用戶發起的而且須要當即獲得結果的任務,好比滑動scroll view時去加載數據用於後續cell的顯示,這些任務一般跟後續的用戶交互相關,在幾秒或者更短的時間內完成。
  • NSQualityOfServiceUtility:一些可能須要花點時間的任務,這些任務不須要立刻返回結果,好比下載的任務,這些任務可能花費幾秒或者幾分鐘的時間。
  • NSQualityOfServiceBackground:這些任務對用戶不可見,好比後臺進行備份的操做,這些任務可能須要較長的時間,幾分鐘甚至幾個小時。
  • NSQualityOfServiceDefault:優先級介於user-initiated 和 utility,當沒有 QoS信息時默認使用,開發者不該該使用這個值來設置本身的任務。

@property NSUInteger stackSize;
複製代碼

線程的堆棧大小,線程執行前堆棧大小爲512K,線程完成後堆棧大小爲0K。注意線程執行完畢後,因爲內存空間被釋放,不能再次啓動。


@property (readonly) BOOL isMainThread;
複製代碼

是不是主線程。


@property (readonly, getter=isExecuting) BOOL executing;
複製代碼

線程是否正在執行。


@property (readonly, getter=isFinished) BOOL finished;
複製代碼

線程是否執行完成。


@property (readonly, getter=isCancelled) BOOL cancelled;
複製代碼

線程是否已經取消。


2.2 類屬性

@property (class, readonly, strong) NSThread *currentThread;
複製代碼

當前線程。


+ (BOOL)isMultiThreaded;
複製代碼

是不是多線程。


@property (class, readonly) BOOL isMainThread;
複製代碼

當前線程是不是主線程。


@property (class, readonly, strong) NSThread *mainThread;
複製代碼

主線程。


3. NSThread 的類方法

// 線程休眠到指定時間
+ (void)sleepUntilDate:(NSDate *)date;

// 線程休眠指定時長
+ (void)sleepForTimeInterval:(NSTimeInterval)ti;

// 退出線程
+ (void)exit;

// 是不是多線程
+ (BOOL)isMultiThreaded;
複製代碼

4. NSThread 其它相關方法

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array;

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait;

- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array;

- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait;

- (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg;
複製代碼
相關文章
相關標籤/搜索