NSThread

作者: CMD独白 | 来源:发表于2016-03-10 15:53 被阅读353次

    使用NSThread开辟线程有两种方式:

    (1)手动开启方式的创建:

    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(thread) object:@"thread"];
    [thread start];//    开启线程
    

    (2)自动开启方式的创建:

    [NSThread detachNewThreadSelector:@selector(thread1:) toTarget:self withObject:@"thread1"];
    - (void)thread1:(NSString *)sender{
    
        [NSThread currentThread];//获取到当前所在的信息
    
        NSThread *thread = [NSThread currentThread];
    
    //    thread.name = @"我是子线程 ";
    
    //    NSLog(@"%@",thread);
    
    ////  [NSThread isMainThread] 判断当前线程是否是主线程
    
    //    BOOL isMainThread = [NSThread isMainThread];
    
    ////  [NSThread isMultiThreaded] 判断是否是多线程
    
    //    BOOL isMUltiThread = [NSThread isMultiThreaded];
    
    //    NSLog(@"%d,%d",isMainThread,isMUltiThread);
    
    ////    设置线程的优先级(0-1) setThreadPriority:
    
    //    [NSThread setThreadPriority:1.0];
    
    ////  sleepForTimeInterval:让线程休眠
    
    //    [NSThread sleepForTimeInterval:2];
    
    //    从网络加载图片并将它转化为data类型的数据
    
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kUrl]];
    
    image = [UIImage imageWithData:data];
    
    //    waiUntilDone设为YES,意味着UI更新完才会做其它操作
    
    [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
    
    }
    

    相关文章

      网友评论

          本文标题:NSThread

          本文链接:https://www.haomeiwen.com/subject/pzutlttx.html