用NSThread建立線程
iOS開發多線程-建立線程
一 建立和啟動線程簡單說明
一個NSThread對象就代表了一條線程
建立,啟動線程
(1)NSThread *thread =[[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];
[thread start];
//線程已啟動,就會線上程thread中執行self的run方法
主線程相關用法
+(NSThread*)mainThread;//獲得主線程
-(BOOL)isMainThread;//是否為主線程
+(BOOL)isMainThread;//是否為主線程
其他用法
獲得目前線程
NSThread *current = [NSThread currentThread];
線程的排程優先級:排程優先級的取值範圍是0.0~1.0,預設是0.5,值越大優先級越高
+(double)threadPriority;
+(BOOL)setThreadPriority:(double)p;
設定線程的名字
-(void)setName:(NSString*)n;
-(NSString *)name;
其他建立線程的方式
(2)建立線程後自動啟動線程 [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
(3)隐式建立并啟動線程 [self performSelectorInBackground:@selector(run) withObject:nil];
上訴2種建立線程方式的優缺點
優點:簡單快捷
缺點:無法對線程進行更詳細的設定
二 代碼示例
1. 使用古老的方式建立
//
// YYViewController.m
//
//
// Created by apple on 14-6-23.
// Copyright (c) 2014年 itcase. All rights reserved.
//
#import "YYViewController.h"
#import <pthread.h>
@interface YYViewController ()
- (IBAction)btnClick;
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
//按鈕的點選事件
- (IBAction)btnClick {
//1.擷取目前線程
NSThread *current=[NSThread currentThread];
//主線程
NSLog(@"btnClick----%@",current);
//2.使用for循環執行一些耗時操作
pthread_t thread;
pthread_create(&thread, NULL, run, NULL);
}
//c語言函數
void *run(void *data)
{
//擷取目前線程,是新建立出來的線程
NSThread *current=[NSThread currentThread];
for (int i=0; i<10000; i++) {
NSLog(@"btnClick---%d---%@",i,current);
}
return NULL;
}
//多個線程,點選按鈕執行按鈕調用方法的時候,主線程沒有被阻塞
@end
實作效果:

列印結果:
2.使用NSThread建立線程
//
// YYViewController.m
//
//
// Created by apple on 14-6-23.
// Copyright (c) 2014年 itcase. All rights reserved.
//
#import "YYViewController.h"
#import <pthread.h>
@interface YYViewController ()
- (IBAction)btnClick;
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
//按鈕的點選事件
- (IBAction)btnClick {
//1.擷取目前線程
NSThread *current=[NSThread currentThread];
//主線程
NSLog(@"btnClick----%@",current);
//擷取主線程的另外一種方式
NSThread *main=[NSThread mainThread];
NSLog(@"主線程-------%@",main);
//2.執行一些耗時操作
[self creatNSThread];
// [self creatNSThread2];
// [self creatNSThread3];
}
-(void)creatNSThread
{
NSThread *thread=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"線程A"];
//為線程設定一個名稱
[email protected]"線程A";
//開啟線程
[thread start];
NSThread *thread2=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"線程B"];
//為線程設定一個名稱
[email protected]"線程B";
//開啟線程
[thread2 start];
}
-(void)creatNSThread2
{
// NSThread *thread=[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"建立完線程直接(自動)啟動"];
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"建立完線程直接(自動)啟動"];
}
-(void)creatNSThread3
{
//在背景線程中執行===在子線程中執行
[self performSelectorInBackground:@selector(run:) withObject:@"隐式建立"];
}
-(void)run:(NSString *)str
{
//擷取目前線程
NSThread *current=[NSThread currentThread];
//列印輸出
for (int i=0; i<10; i++) {
NSLog(@"run---%@---%@",current,str);
}
}
@end
調用線程1,列印結果為:
調用線程2
調用線程3