//创建线程对象
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(demo) object:nil];
//开启线程
[thread start];
方式2(类方法)
[NSThread detachNewThreadSelector:@selector(demo) toTarget:self withObject:nil];
方式3(隐式创建)
[self performSelectorInBackground:@selector(demo) withObject:nil];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//创建线程
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(demo) object:nil];
//开启线程 -- 就绪状态
[thread start];
}
- (void)demo{
//睡眠 -- 阻塞状态
[NSThread sleepForTimeInterval:1];
//就绪
for (int i = 0; i< 20; i++) {
if (i == 5) {
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
NSLog(@"我睡醒了");
}
NSLog(@"hello %@ %d",[NSThread currentThread],i);
if(i == 10){
//退出线程 -- 死亡状态
NSLog(@"ni死了");
[NSThread exit];
}
}
}
thread.name = @"myThread"; //线程的名字
线程的优先级 threadPriority:有更大几率被CPU执行到,不保证先被执行到
thread.threadPriority = 1; //线程的优先级 0.0-1.0 1.0的优先级最高
判断当前是不是主线程
[[NSThread currentThread] isMainThread]
多个线程同时对同一个变量进行读和写会出现数据问题
卖票
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(buyTicket) object:nil];
[thread start];
NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(buyTicket) object:nil];
[thread1 start];
}
- (void)buyTicket{
while (YES) {
[NSThread sleepForTimeInterval:1];
//进入 锁上1
if (self.tickets > 0) {
self.tickets--;
NSLog(@"卖了一张票%@, %d",[NSThread currentThread],self.tickets);
continue;
}
//出去 打开锁0
NSLog(@"你来晚了卖晚了");
break;
}
@synchronized(NSObject){}
- (void)buyTicket{
while (YES) {
[NSThread sleepForTimeInterval:1];
//每一个对象内部都有一把锁,默认锁是打开1 锁上0
//互斥锁 ---> 线程同步
@synchronized(self){
//进入 锁上1
if (self.tickets > 0) {
self.tickets--;
NSLog(@"卖了一张票%@, %d",[NSThread currentThread],self.tickets);
continue;
}
//出去 打开锁0
}
NSLog(@"你来晚了卖晚了");
break;
}
@property(atomic) NSObject *obj;
保证数据的正确性为第一优先,可以损失点性能
线程安全:在多个线程进行读写操作时,仍然能够保证数据的正确。 线程不安全:多个线程对同一个全局变量进行读写操作,会产生问题。
//初始化控制器内部的视图 storyboard 或者xib
- (void)loadView{
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
scrollView.backgroundColor = [UIColor grayColor];
self.scrollView = scrollView;
self.view = scrollView;
UIImageView *imageView = [[UIImageView alloc] init];
self.imageView = imageView;
//addSubview 把控件添加到了self.view.subviews 数组中
[self.view addSubview:self.imageView];
}
- (void)downloadImage{
//加载一张网络图片
NSURL *url = [NSURL URLWithString:@"http://f.hiphotos.baidu.com/image/pic/item/4610b912c8fcc3ce6ea8e7d09045d688d53f20ec.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
self.image = [[UIImage alloc] initWithData:data];
NSLog(@"---%@",[NSThread currentThread]);
}
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(downloadImage) object:nil];
[thread start];
[self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:YES];
- (void)updateUI{
NSLog(@"%@",[NSThread currentThread]);
// [NSThread sleepForTimeInterval:1];
NSLog(@"updateUI");
//图片下载完成
self.imageView.image = self.image;
//让imageView的大小和它内部的image的大小一样
[self.imageView sizeToFit];
self.scrollView.contentSize = self.image.size;
}
定义:多个线程之间需要经常进行通信
使用场景:
常用方法:
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait;