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();
}
}