多線程從入門到放棄

三大常用法寶

本文章著重介紹多線程的基本用法妆艘,本文件的例子是下載圖片為例子殷蛇,子線程下載圖片谱轨,主線程更新圖片

經(jīng)典案例

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    子線程下載任務(wù)
    dispatch_async(dispatch_get_main_queue(), ^{
       主線程更新UI
    });
});

NSThread

//動(dòng)態(tài)創(chuàng)建線程
-(void)dynamicCreateThread{
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(loadImageSource:) object:imgUrl];
    thread.threadPriority = 1;// 設(shè)置線程的優(yōu)先級(0.0 - 1.0,1.0最高級)
    [thread start];
}

//靜態(tài)創(chuàng)建線程
-(void)staticCreateThread{
    [NSThread detachNewThreadSelector:@selector(loadImageSource:) toTarget:self withObject:imgUrl];
}

//隱式創(chuàng)建線程
-(void)implicitCreateThread{
    [self performSelectorInBackground:@selector(loadImageSource:) withObject:imgUrl];
}

-(void)loadImageSource:(NSString *)url{
    NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
    UIImage *image = [UIImage imageWithData:imgData];
    if (imgData!=nil) {
        [self performSelectorOnMainThread:@selector(refreshImageView:) withObject:image waitUntilDone:YES];
    }else{
        NSLog(@"there no image data");
    }
}

-(void)refreshImageView:(UIImage *)image{
    [self.loadingLb setHidden:YES];
    [self.imageView setImage:image];
}

NSOperation & NSOperationQueue

//使用子類NSInvocationOperation
-(void)useInvocationOperation{
    NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadImageSource:) object:imgUrl];
    //[invocationOperation start];//直接會(huì)在當(dāng)前線程主線程執(zhí)行
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [queue addOperation:invocationOperation];
    
}

//使用子類NSBlockOperation
-(void)useBlockOperation{
    
    NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
        [self loadImageSource:imgUrl];
    }];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [queue addOperation:blockOperation];
    
}
//使用繼承NSOperation
-(void)useSubclassOperation{
    
    LoadImageOperation *imageOperation = [LoadImageOperation new];
    imageOperation.loadDelegate = self;
    imageOperation.imgUrl = imgUrl;
    
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [queue addOperation:imageOperation];
}

-(void)loadImageSource:(NSString *)url{
    
    NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
    UIImage *image = [UIImage imageWithData:imgData];
    if (imgData!=nil) {
        [self performSelectorOnMainThread:@selector(refreshImageView1:) withObject:image waitUntilDone:YES];
    }else{
        NSLog(@"there no image data");
    }
    
}

-(void)refreshImageView1:(UIImage *)image{
    [self.loadingLb setHidden:YES];
    [self.imageView setImage:image];
}

Deme鏈接:https://github.com/DuffYang/PerfectMutiThread

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末浙炼,一起剝皮案震驚了整個(gè)濱河市份氧,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌弯屈,老刑警劉巖蜗帜,帶你破解...
    沈念sama閱讀 221,576評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異资厉,居然都是意外死亡厅缺,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評論 3 399
  • 文/潘曉璐 我一進(jìn)店門酌住,熙熙樓的掌柜王于貴愁眉苦臉地迎上來店归,“玉大人,你說我怎么就攤上這事酪我。” “怎么了且叁?”我有些...
    開封第一講書人閱讀 168,017評論 0 360
  • 文/不壞的土叔 我叫張陵都哭,是天一觀的道長。 經(jīng)常有香客問我逞带,道長欺矫,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,626評論 1 296
  • 正文 為了忘掉前任展氓,我火速辦了婚禮穆趴,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘遇汞。我一直安慰自己未妹,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,625評論 6 397
  • 文/花漫 我一把揭開白布空入。 她就那樣靜靜地躺著络它,像睡著了一般。 火紅的嫁衣襯著肌膚如雪歪赢。 梳的紋絲不亂的頭發(fā)上化戳,一...
    開封第一講書人閱讀 52,255評論 1 308
  • 那天,我揣著相機(jī)與錄音埋凯,去河邊找鬼点楼。 笑死扫尖,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的掠廓。 我是一名探鬼主播换怖,決...
    沈念sama閱讀 40,825評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼却盘!你這毒婦竟也來了狰域?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,729評論 0 276
  • 序言:老撾萬榮一對情侶失蹤黄橘,失蹤者是張志新(化名)和其女友劉穎兆览,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體塞关,經(jīng)...
    沈念sama閱讀 46,271評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡抬探,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,363評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了帆赢。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片小压。...
    茶點(diǎn)故事閱讀 40,498評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖椰于,靈堂內(nèi)的尸體忽然破棺而出怠益,到底是詐尸還是另有隱情,我是刑警寧澤瘾婿,帶...
    沈念sama閱讀 36,183評論 5 350
  • 正文 年R本政府宣布蜻牢,位于F島的核電站,受9級特大地震影響偏陪,放射性物質(zhì)發(fā)生泄漏抢呆。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,867評論 3 333
  • 文/蒙蒙 一笛谦、第九天 我趴在偏房一處隱蔽的房頂上張望抱虐。 院中可真熱鬧,春花似錦饥脑、人聲如沸恳邀。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,338評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽轩娶。三九已至,卻和暖如春框往,著一層夾襖步出監(jiān)牢的瞬間鳄抒,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,458評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留许溅,地道東北人瓤鼻。 一個(gè)月前我還...
    沈念sama閱讀 48,906評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像贤重,于是被迫代替她去往敵國和親茬祷。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,507評論 2 359

推薦閱讀更多精彩內(nèi)容