NSThread

使用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];

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容