多線程_NSThread

方法1:

-(void)test2
{
    NSThread *thread=[[NSThread alloc] initWithTarget:self
                                     selector:@selector(run)                                                   
                                     object:nil];//參數
    //讓線程開始工作
    [thread start];
}
-(void)run
{
    //線程任務
    NSLog(@"%@",[NSThread currentThread]);
}

方法2:

//創建線程2
-(void)test3
{
    [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"heilo"];
}
-(void)run:(NSString *)string
{
    //線程任務
    NSLog(@"%@,%@",[NSThread currentThread],string);
}

方法3:

-(void)test4
{
    //隱式創建線程
    [self performSelectorInBackground:@selector(run2:) withObject:@"aa"];
}
-(void)run2:(NSString *)string
{
    NSLog(@"%@",string);
}

上面兩個:
缺點:需要開啓線程
優點:對象可見可以對線程進行操作

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章