iOS多線程開發小demo2,NSThread篇

來源:互聯網
上載者:User

標籤:class   blog   tar   get   2014   string   

用NSThread建立子線程的3種方法

//  DYFViewController.m//  623-02-pthread////  Created by dyf on 14-6-23.//  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.//#import "DYFViewController.h"#import <pthread.h>@interface DYFViewController ()@end@implementation DYFViewController//// c語言函數//void *run(void *data)//{//    // 1.擷取當前的線程//    NSThread *cThread = [NSThread currentThread];//    //    // 2.列印線程//    NSLog(@"%@", cThread);//    //    // 3.h耗時操作//    for (int i = 0; i < 9999; i++) {//        NSLog(@"%@", cThread);//    }////    return NULL;//}- (IBAction)btnOnClick {    // 1.擷取當前的線程    NSThread *cthread = [NSThread currentThread];        NSThread *mt = [NSThread mainThread];    // 2.列印線程    NSLog(@"%@", cthread);        NSLog(@"%@", mt);        // 3.執行一線耗時的操作 : 建立一套子線程    [self threadCreate3];  }- (void)run:(NSString *)parma{//    [NSThread threadPriority];//    //    [NSThread setThreadPriority:0.55];    // 取值0-1,預設0.5    for (int i = 0; i < 9999; i++) {        NSLog(@"%@---------%@", [NSThread currentThread], parma);    }}- (void)threadCreate5{    // 分離出的子線程    [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"2222222"];}- (void)threadCreate4{    // 分離出的子線程    [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"2222222"];}/** *  NSThread建立方式3:隱世線程建立,並且直接(自動)啟動 */- (void)threadCreate3{    [self performSelectorInBackground:@selector(run:) withObject:@"333333"];}/** *  建立方式2:建立完線程後自動啟動 */- (void)threadCreate2{    // 分離出的子線程    [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"2222222"];}/** *  建立方式1:①先建立初始化子線程②再啟動 */- (void)threadCreate{    NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"heheh"];    thread1.name = @"thread1";    // 開啟線程    [thread1 start];        NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"heheh"];    thread2.name = @"thread2";    // 開啟線程    [thread2 start];        NSThread *thread3 = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"heheh"];    thread3.name = @"33";    // 開啟線程    [thread3 start];}@end

 利用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.