iOS 建立多線程的三種方法,ios多線程三種方法

來源:互聯網
上載者:User

iOS 建立多線程的三種方法,ios多線程三種方法

(1)//通過NSObject的方法建立線程</strong></span>    //(這個方法會自動開闢一個後台線程,參數1:在這個後台線程中執行的方法,參數2:用於傳遞參數)    [self performSelectorInBackground:@selector(banZhuanPlus) withObject:nil];(2)//通過NSThread建立線程(參數1:方法的執行者;參數2:線上程中執行的方法;參數3:用於傳遞參數)    //第一步:建立線程    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(banZhuanPlus) object:nil];    //第二步:執行    [thread start];    [thread release];  (3)//NSOperation就是一個操作單元,用來執行方法,是一個抽象類別,必須子類化或者使用系統建立好的子類(NSInvocationOperation or NSBlockOperation)    //NSOperation是最小的操作單元;只能夠執行一次;        //NSInvocationOperation第一步:建立    NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(banZhuanPlus) object:nil];    //第二步:(不設定的話不添加到隊列)在主線程中執行//    [invocation start];        //NSBlockOperation第一步:建立    NSBlockOperation *block = [NSBlockOperation blockOperationWithBlock:^{        [self banZhuanPlus];    }];//    //第二步:執行(在主線程中執行)//    [block start];//如果添加到隊列就不要start了        // 這個隊列會自動幫咱們建立一個輔助的線程    //這個隊列裡面只能夠添加NSOperation以及子類的對象;    NSOperationQueue *queue = [[NSOperationQueue alloc] init];    [queue setMaxConcurrentOperationCount:2];//設定最大並行數;    [queue addOperation:block];//只要把操作隊列添加到隊列中就會執行;    [queue addOperation:invocation];        //隊列: 先進先出    //棧: 先進後出        //隊列中涉及到串列和並行    //串列: 一次只能執行一個任務    //並行: 一次可以執行多個任務(整片複製的時候,注意沒有注釋的屬於一體)      



iOS 多線程怎實現

你問錯地方了~~IOS是Object-C不是JAVA
 
怎建立多線程

兩種方式,第一種繼承Thread,第二種實現Runnable
public Class Threadone extends Thread{
public void run(){//你的實現代碼}
}

public class Threadtwo implements Runnable{
public void run(){//你的實現代碼}
}
這兩種方式的調用:
public class A {
public static void main(String[]args){
//第一種的調用方式
Threadone one = new Threadone();
one.start();

//第二種的調用方式
Theadtwo two = new Threadtwo();
Thread thread = new Thread(two);
thread.start();

}
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.