iOS多線程初見,ios多線程

來源:互聯網
上載者:User

iOS多線程初見,ios多線程

1. 三種建立線程的方法

//第一種

     NSThread * thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(doAction) object:nil];

    //線程名

    thread1.name = @"thread1";

    //線程優先順序,0 ~ 1

    thread1.threadPriority = 1.0;

    //開啟線程

    [thread1 start];

//第二種

    //通過類方法建立線程,不用顯示的開啟start

    [NSThread detachNewThreadSelector:@selector(doAction) toTarget:self withObject:nil];

//第三種

    //隱式建立多線程

    [self performSelectorInBackground:@selector(doAction:) withObject:nil];

    

 

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

 

    NSLog(@"mainThread - %@",[NSThread mainThread]);

 

    NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(handleAction) object:nil];

    

    //就緒狀態

    [thread start];

}

 

- (void)handleAction {

    

    for (NSInteger i = 0 ; i < 100; i ++) {

        

        //阻塞狀態

//        [NSThread sleepForTimeInterval:2];

//        [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];

        

//        NSLog(@"%@,%@",[NSThread currentThread],@(i));

        //可以在子線程擷取主線程

        NSLog(@"mainThread - %@",[NSThread mainThread]);    

        if (i == 10) {

            //退出

            [NSThread exit];

        } 

    }

}

3.同步代碼塊實現買票功能

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    self.tickets = 20;

    

    NSThread * thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];

//    thread1.name = @"computer";

    

    [thread1 start];

 

    

    NSThread * thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];

//    thread2.name = @"phone";

    

    [thread2 start];

}

 

- (void)saleTicket {

    

    

    while (1) {

    

//        [NSThread sleepForTimeInterval:1];

        

        

        //token必須所有線程都能訪問到,一般用self

        

//        @synchronized() {

        

            //程式碼片段

//        }

        

        

//        NSObject * o = [[NSObject alloc] init];

        

        //互斥鎖

        @synchronized(self) {

            

            [NSThread sleepForTimeInterval:2];

            

            if (self.tickets > 0) {

                

                NSLog(@"%@ 還有餘票 %@ 張",[NSThread currentThread],@(self.tickets));

                

                self.tickets -- ;

                

            } else {

                

                NSLog(@"票賣完了");

                

                break;

            }

            

        }

  

    }

    

}

 

相關文章

聯繫我們

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