您好,登錄后才能下訂單哦!
多線程有4種
NSObject---NSObject自帶的,但是他不能對數據進行保護
NSThread ---過于復雜,使用起來不夠方便
NSOperationQueue ---操作隊列,管理線程,內部有一個線程池,負責對現有的線程進行管理/重用
GDC(grand central dispatch); ----基于C的多線程解決方案
隊列有兩種(串行/并行)
MainViewController.m
#import "MainViewController.h" #import "MyOperation.h" @interface MainViewController () @property (nonatomic , retain)UIImageView *p_w_picpathView; @end @implementation MainViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(110, 100, 100, 30)]; [button setTitle:@"按鈕" forState:UIControlStateNormal]; button.backgroundColor = [UIColor orangeColor]; button.layer.cornerRadius = 5; [self.view addSubview:button]; [button addTarget:self action:@selector(GCDAction:) forControlEvents:UIControlEventTouchUpInside]; [button release]; self.p_w_picpathView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 150, 240, 300)]; self.p_w_picpathView.backgroundColor = [UIColor cyanColor]; [self.view addSubview:self.p_w_picpathView]; [_p_w_picpathView release]; } - (void)buttonClick:(UIButton *)button { NSLog(@"判斷當前線程是不是主線程%d",[NSThread isMainThread]); //讓當前的線程休眠3秒 [NSThread sleepForTimeInterval:3]; NSLog(@"休眠結束"); NSLog(@"當前線程: %@",[NSThread currentThread]) ; int a = 0; for (int i = 0 ; i < 600000000; i++) { a++; } NSLog(@"%d",a); } - (void)NSObjectAction:(UIButton *)button { //1.多線程的實現方式:NSObject自帶的 //綁定一個方法,然后給他一個參數 優點:簡介快速,缺點:不能對數據進行保護 [self performSelectorInBackground:@selector(buttonClick:) withObject:button]; } - (void)threadAciton:(UIButton *)button { //2.NStrread類 //這個類ude一個對象,就代表一個線程 優點:可以控制線程的所有方面 缺點:太復雜,使用起來不方便 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(buttonClick:) object:button]; thread.name = @"劉亞芳"; //開始執行子線程 [thread start]; [thread release]; } - (void)operationAction:(UIButton *)button { //NSOperation類 //本身不能夠實現多線程操作,代表一個任務(進程),安排在某個線程里面 MyOperation *opreation = [[MyOperation alloc] init]; //開始執行任務,執行的任務會在當前執行 [opreation start]; [opreation release]; // NSInvocationOperation *op1 = [NSInvocationOperation alloc] initWithTarget:<#(id)#> selector:<#(SEL)#> object:<#(id)#> // NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:<#^(void)block#>] } - (void)poerationQueueAction:(UIButton *)button { //NSOperationQueue 操作隊列(管理線程,線程池) 隊列有兩類:(并行/串行) //內部有一個線程池,負責對現有的線程進行管理/重用 //創建一個新的隊列 //主隊列是一個串行隊列 NSOperationQueue *queue = [[NSOperationQueue alloc] init]; //設置一個最大并發數(如果最大并發數為1,那么就為串行,否則為并行) [queue setMaxConcurrentOperationCount:1]; //向隊列中添加任務 MyOperation *op1 = [[MyOperation alloc] init]; MyOperation *op2 = [[MyOperation alloc] init]; MyOperation *op3 = [[MyOperation alloc] init]; MyOperation *op4 = [[MyOperation alloc] init]; [queue addOperation:op1]; [queue addOperation:op2]; [queue addOperation:op3]; [queue addOperation:op4]; [op1 release]; [op2 release]; [op3 release]; [op4 release]; } - (void)GCDAction:(UIButton *)button { //GCD: grand central dispatch 基于C的多線程解決方案 //1.創建一個調度隊列 //參數1: 隊列名字 參數2:隊列的類型(串行/并行) //DISPATCH_QUEUE_CONCURRENT---并行隊列 //DISPATCH_QUEUE_SERIAL ---串行隊列 dispatch_queue_t myQueue = dispatch_queue_create("liuyafang", DISPATCH_QUEUE_SERIAL); //2.使用的隊列 //參數1:要在哪個隊列執行 參數2:要執行的內容 dispatch_async(myQueue, ^{ //任務1 [self buttonClick:button]; }); dispatch_async(myQueue, ^{ //任務2 NSLog(@"==========="); }); //使用系統的隊列 //系統提供了5個隊列,1個串行隊列(主隊列),4個并行隊列(全局隊列) //獲取系統的主隊列 dispatch_queue_t mainQueue = dispatch_get_main_queue(); dispatch_async(mainQueue, ^{ //執行在主隊列中要做的任務 //全部的UI任務(reloadDate ,給試圖賦值) }); //獲取全局隊列 //參數1:獲取哪一個全局隊列 參數2:給未來使用的(必須填寫0) dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); //GCD的常見用法 //在全局隊列中請求數據 dispatch_async(globalQueue, ^{ NSString *str = @"https://cache.yisu.com/upload/information/20200312/67/251264.jpg"; NSURL *url = [NSURL URLWithString:str]; //請求網絡地址數據的同步方法 //因為這個方法在子線程(全局隊列)中執行,所以不需要考慮死線程的問題 NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *p_w_picpath = [UIImage p_w_picpathWithData:data]; //所有刷新UI的操作都必須回到主隊列進行(跳回到子線程) dispatch_async(mainQueue, ^{ self.p_w_picpathView.p_w_picpath = p_w_picpath; }); }); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ NSLog(@"喬老爺不該死啊!!!"); }); //一段代碼值執行一次,(單列的創建) static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSLog(@"只執行一次"); }); } - (void)dealloc { [_p_w_picpathView release]; [super dealloc]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
MyOperation.h
看繼承關系---這個是繼承與NSOperation
#import <Foundation/Foundation.h> @interface MyOperation : NSOperation @end
MyOperation.m
#import "MyOperation.h" @implementation MyOperation - (void)main { //自己定義的Operation的類,需要在mian方法中寫要執行的任務內容 NSLog(@"當前線程:%@",[NSThread currentThread]); NSLog(@"是不是主線程尼:%d",[NSThread isMainThread]); int a = 0; for (int i = 0 ; i < 600000000; i++) { a++; } NSLog(@"%d",a); } @end
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。