您好,登錄后才能下訂單哦!
多線程有4種
NSObject---NSObject自帶的,但是他不能對數(shù)據(jù)進(jìn)行保護(hù)
NSThread ---過于復(fù)雜,使用起來不夠方便
NSOperationQueue ---操作隊(duì)列,管理線程,內(nèi)部有一個(gè)線程池,負(fù)責(zé)對現(xiàn)有的線程進(jìn)行管理/重用
GDC(grand central dispatch); ----基于C的多線程解決方案
隊(duì)列有兩種(串行/并行)
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āng)前線程是不是主線程%d",[NSThread isMainThread]); //讓當(dāng)前的線程休眠3秒 [NSThread sleepForTimeInterval:3]; NSLog(@"休眠結(jié)束"); NSLog(@"當(dāng)前線程: %@",[NSThread currentThread]) ; int a = 0; for (int i = 0 ; i < 600000000; i++) { a++; } NSLog(@"%d",a); } - (void)NSObjectAction:(UIButton *)button { //1.多線程的實(shí)現(xiàn)方式:NSObject自帶的 //綁定一個(gè)方法,然后給他一個(gè)參數(shù) 優(yōu)點(diǎn):簡介快速,缺點(diǎn):不能對數(shù)據(jù)進(jìn)行保護(hù) [self performSelectorInBackground:@selector(buttonClick:) withObject:button]; } - (void)threadAciton:(UIButton *)button { //2.NStrread類 //這個(gè)類ude一個(gè)對象,就代表一個(gè)線程 優(yōu)點(diǎn):可以控制線程的所有方面 缺點(diǎn):太復(fù)雜,使用起來不方便 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(buttonClick:) object:button]; thread.name = @"劉亞芳"; //開始執(zhí)行子線程 [thread start]; [thread release]; } - (void)operationAction:(UIButton *)button { //NSOperation類 //本身不能夠?qū)崿F(xiàn)多線程操作,代表一個(gè)任務(wù)(進(jìn)程),安排在某個(gè)線程里面 MyOperation *opreation = [[MyOperation alloc] init]; //開始執(zhí)行任務(wù),執(zhí)行的任務(wù)會(huì)在當(dāng)前執(zhí)行 [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 操作隊(duì)列(管理線程,線程池) 隊(duì)列有兩類:(并行/串行) //內(nèi)部有一個(gè)線程池,負(fù)責(zé)對現(xiàn)有的線程進(jìn)行管理/重用 //創(chuàng)建一個(gè)新的隊(duì)列 //主隊(duì)列是一個(gè)串行隊(duì)列 NSOperationQueue *queue = [[NSOperationQueue alloc] init]; //設(shè)置一個(gè)最大并發(fā)數(shù)(如果最大并發(fā)數(shù)為1,那么就為串行,否則為并行) [queue setMaxConcurrentOperationCount:1]; //向隊(duì)列中添加任務(wù) 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.創(chuàng)建一個(gè)調(diào)度隊(duì)列 //參數(shù)1: 隊(duì)列名字 參數(shù)2:隊(duì)列的類型(串行/并行) //DISPATCH_QUEUE_CONCURRENT---并行隊(duì)列 //DISPATCH_QUEUE_SERIAL ---串行隊(duì)列 dispatch_queue_t myQueue = dispatch_queue_create("liuyafang", DISPATCH_QUEUE_SERIAL); //2.使用的隊(duì)列 //參數(shù)1:要在哪個(gè)隊(duì)列執(zhí)行 參數(shù)2:要執(zhí)行的內(nèi)容 dispatch_async(myQueue, ^{ //任務(wù)1 [self buttonClick:button]; }); dispatch_async(myQueue, ^{ //任務(wù)2 NSLog(@"==========="); }); //使用系統(tǒng)的隊(duì)列 //系統(tǒng)提供了5個(gè)隊(duì)列,1個(gè)串行隊(duì)列(主隊(duì)列),4個(gè)并行隊(duì)列(全局隊(duì)列) //獲取系統(tǒng)的主隊(duì)列 dispatch_queue_t mainQueue = dispatch_get_main_queue(); dispatch_async(mainQueue, ^{ //執(zhí)行在主隊(duì)列中要做的任務(wù) //全部的UI任務(wù)(reloadDate ,給試圖賦值) }); //獲取全局隊(duì)列 //參數(shù)1:獲取哪一個(gè)全局隊(duì)列 參數(shù)2:給未來使用的(必須填寫0) dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); //GCD的常見用法 //在全局隊(duì)列中請求數(shù)據(jù) dispatch_async(globalQueue, ^{ NSString *str = @"https://cache.yisu.com/upload/information/20200312/67/251264.jpg"; NSURL *url = [NSURL URLWithString:str]; //請求網(wǎng)絡(luò)地址數(shù)據(jù)的同步方法 //因?yàn)檫@個(gè)方法在子線程(全局隊(duì)列)中執(zhí)行,所以不需要考慮死線程的問題 NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *p_w_picpath = [UIImage p_w_picpathWithData:data]; //所有刷新UI的操作都必須回到主隊(duì)列進(jìn)行(跳回到子線程) 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(@"喬老爺不該死啊!!!"); }); //一段代碼值執(zhí)行一次,(單列的創(chuàng)建) static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSLog(@"只執(zhí)行一次"); }); } - (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
看繼承關(guān)系---這個(gè)是繼承與NSOperation
#import <Foundation/Foundation.h> @interface MyOperation : NSOperation @end
MyOperation.m
#import "MyOperation.h" @implementation MyOperation - (void)main { //自己定義的Operation的類,需要在mian方法中寫要執(zhí)行的任務(wù)內(nèi)容 NSLog(@"當(dāng)前線程:%@",[NSThread currentThread]); NSLog(@"是不是主線程尼:%d",[NSThread isMainThread]); int a = 0; for (int i = 0 ; i < 600000000; i++) { a++; } NSLog(@"%d",a); } @end
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。