iOS-多線程 ,整理集錦,多種線程的建立

來源:互聯網
上載者:User

標籤:style   java   color   使用   os   art   

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];        //建立線程的第一種方式    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"universe"];    [thread start];    [thread release];            //建立線程的第二種方式,NSThread類方法    [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"yuzhou"];            //建立線程的第三種方法  NSObject方法    [self performSelectorInBackground:@selector(run:) withObject:@"nsobject thread"];        //建立線程的第四種方式    NSOperationQueue *oprationQueue = [[NSOperationQueue alloc] init];    [oprationQueue addOperationWithBlock:^{        //這個block語句塊在子線程中執行        NSLog(@"oprationQueue");    }];    [oprationQueue release];        //第五種建立線程的方式    NSOperationQueue *oprationQueue1 = [[NSOperationQueue alloc] init];    oprationQueue1.maxConcurrentOperationCount = 1;//指定池子的並發數        //NSOperation 相當於java中的runnable介面,繼承自它的就相當一個任務    NSInvocationOperation *invation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run:) object:@"invation"];    [oprationQueue1 addOperation:invation];//將任務添加到池子裡面,可以給池子添加多個任務,並且指定它的並發數    [invation release];        //第二個任務    NSInvocationOperation *invation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run2:) object:@"invocation2"];    invation2.queuePriority = NSOperationQueuePriorityHigh;//設定線程優先順序    [oprationQueue1 addOperation:invation2];    [invation2 release];        [oprationQueue1 release];        //調用主線程,用來子線程和主線程互動,最後面的那個boolean參數,如果為yes就是等這個方法執行完了在執行後面的代碼;如果為no的話,就是不管這個方法執行完了沒有,接著往下走    [self performSelectorOnMainThread:@selector(onMain) withObject:self waitUntilDone:YES];        //---------------------GCD----------------------支援多核,高效率的多線程技術    //建立多線程第六種方式    dispatch_queue_t queue = dispatch_queue_create("name", NULL);    //建立一個子線程    dispatch_async(queue, ^{        // 子線程code... ..        NSLog(@"GCD多線程");                //回到主線程        dispatch_sync(dispatch_get_main_queue(), ^{//其實這個也是在子線程中執行的,只是把它放到了主線程的隊列中            Boolean isMain = [NSThread isMainThread];            if (isMain) {                NSLog(@"GCD主線程");            }        });    });            self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    return YES;}- (void)onMain{    Boolean b = [NSThread isMainThread];    if (b) {        NSLog(@"onMain;;%d",b);    }}- (void) run:(NSString*)str{    NSLog(@"多線程運行:::%@",str);}- (void) run2:(NSString*)str{    NSLog(@"多線程運行:::%@",str);}



這些都是基本的線程建立,用NSThread來進行建立線程比較簡單,如果是單一的建立線程可以用NSThread。直接建立可以使用。只需把線程執行的函數和方法寫入即可建立一個線程出來。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.