主線程執(zhí)行耗時操作 阻塞主線程
1-主線程執(zhí)行耗時操作阻塞用戶與UI的交互.png
- (IBAction)buttonClick {
for (NSInteger i = 0; i<50000; i++) {
NSLog(@"------buttonClick---%zd", i);
}
}
用戶點擊了按鈕,在打印完50000個數(shù)字之前与斤,用戶與頁面UI控件的交互被阻塞
pthread
#import "ViewController.h"
#import <pthread.h>
@interface ViewController ()
@end
@implementation ViewController
void * run(void *param)
{
for (NSInteger i = 0; i<50000; i++) {
NSLog(@"------buttonClick---%zd--%@", i, [NSThread currentThread]);
}
return NULL;
}
- (IBAction)buttonClick:(id)sender {
pthread_t thread;
pthread_create(&thread, NULL, run, NULL);
pthread_t thread2;
pthread_create(&thread2, NULL, run, NULL);
}
@end
pthread:純C
一套通用的多線程API
適用于Unix\Linux\Windows等系統(tǒng)
跨平臺\可移植
使用難度大,程序員管理線程生命周期霜旧,在實際開發(fā)中幾乎不用