我和你吻别在无人的街, 在人间离别,我和你吻别在情人的切,,在茫茫人海之中的哥哥啊
iiii
iIII地方sdf
的说法是df
阿斯顿发送到f
啊sdfasdf asdfas
撒的发生
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (nonatomic, strong) NSOperationQueue *operationQueue;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)buttonAction:(UIButton *)sender
{
// 1.NSThread
/*
// 创建一个子线程,去执行test方法
// NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(test:) object:@"子线程"];
// [thread start];
// 直接使用类方法,开启子线程
// [NSThread detachNewThreadSelector:@selector(test:) toTarget:self withObject:@"传递的参数"];
// [self loadImage];
// [NSThread detachNewThreadSelector:@selector(loadImage) toTarget:self withObject:nil];
*/
// 2.NSOperation
/*
// 通过NSInvocationOperation创建任务,通过target-action模式
NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test:) object:nil];
// [invocation start];
// 通过NSBlockOpearation创建任务,通过block方式
NSBlockOperation *block = [NSBlockOperation blockOperationWithBlock:^{
for (int i = 0; i < 10000; i++) {
// NSLog(@"%@ %d", [NSThread currentThread], i);
NSLog(@"block");
}
}];
// [block start];
NSBlockOperation *block2 = [NSBlockOperation blockOperationWithBlock:^{
for (int i = 0; i < 10000; i++) {
// NSLog(@"%@ %d", [NSThread currentThread], i);
NSLog(@"block2");
}
}];
NSBlockOperation *block3 = [NSBlockOperation blockOperationWithBlock:^{
for (int i = 0; i < 10000; i++) {
// NSLog(@"%@ %d", [NSThread currentThread], i);
NSLog(@"block3");
}
}];
NSBlockOperation *block4 = [NSBlockOperation blockOperationWithBlock:^{
for (int i = 0; i < 10000; i++) {
// NSLog(@"%@ %d", [NSThread currentThread], i);
NSLog(@"block4");
}
}];
NSBlockOperation *block5 = [NSBlockOperation blockOperationWithBlock:^{
for (int i = 0; i < 10000; i++) {
// NSLog(@"%@ %d", [NSThread currentThread], i);
NSLog(@"block5");
}
}];
// 任务队列
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
self.operationQueue = operationQueue;
// 设置通知执行的最大数
operationQueue.maxConcurrentOperationCount = 5;
// 设置依赖关系(当block4执行完成之后,再去执行block3)
// [block3 addDependency:block4];
#warning 在添加任务之前做设置
[operationQueue addOperation:invocation];
[operationQueue addOperation:block];
[operationQueue addOperation:block2];
[operationQueue addOperation:block3];
[operationQueue addOperation:block4];
[operationQueue addOperation:block5];
*/
// 3.GCD(任务、分发队列)
/*
* 串行队列:当一个任务执行完成之后,另一个任务才可以执行
* 并行队列:任务在派发的时候是有顺序的,但是在执行的时候是无顺序的,不用等待上一个任务执行完成之后执行
* GCD中的队列分为两种:Serial Queue 、 ConcurrentQueue
* GCD中的队列又可分为三种:主队列、全局队列、自定义队列
*/
/*
* 脱离线程:当线程内部代码执行完毕之后,线程自动关闭。例如:NSThread/Background
* 非脱离线程:当线程内部代码执行完毕之后,线程不关闭,等待着下一次的使用:NSOperationQueue
*/
// 3.1.创建串行队列,并添加任务
// dispatch_queue_t mySerialQueue = dispatch_queue_create("com.lanou3g.mySerialQueue", DISPATCH_QUEUE_SERIAL);
//
// dispatch_async(mySerialQueue, ^{
// for (int i = 0; i < 10000; i++) {
// NSLog(@"任务1");
// }
// });
// dispatch_async(mySerialQueue, ^{
// for (int i = 0; i < 10000; i++) {
// NSLog(@"任务2");
// }
// });
// dispatch_async(mySerialQueue, ^{
// for (int i = 0; i < 10000; i++) {
// NSLog(@"任务3 %@", [NSThread currentThread]);
// }
// });
// 3.2.创建并行队列
// dispatch_queue_t myConcurrentQueue = dispatch_queue_create("com.lanou3g.myConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
//
// dispatch_async(myConcurrentQueue, ^{
// for (int i = 0; i < 10000; i++) {
// NSLog(@"任务1");
// }
// });
//
// dispatch_async(myConcurrentQueue, ^{
// for (int i = 0; i < 10000; i++) {
// NSLog(@"任务2");
// }
// });
//
// dispatch_async(myConcurrentQueue, ^{
// for (int i = 0; i < 10000; i++) {
// NSLog(@"任务3");
// }
// });
// 3.3.使用系统提供的串行队列
// dispatch_queue_t serialQueue = dispatch_get_main_queue();
//
// dispatch_async(serialQueue, ^{
// for (int i = 0; i < 10000; i++) {
// NSLog(@"任务1 %@", [NSThread currentThread]);
// }
// });
//
// dispatch_async(serialQueue, ^{
// for (int i = 0; i < 10000; i++) {
// NSLog(@"任务2");
// }
// });
//
// dispatch_async(serialQueue, ^{
// for (int i = 0; i < 10000; i++) {
// NSLog(@"任务3");
// }
// });
// 3.4.获取系统提供的并行队列
// dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 优先级、暂时无用
//
// dispatch_async(concurrentQueue, ^{
// for (int i = 0; i < 10000; i++) {
// NSLog(@"任务1: %@", [NSThread currentThread]);
// }
// });
//
// dispatch_async(concurrentQueue, ^{
// for (int i = 0; i < 10000; i++) {
// NSLog(@"任务2: %@", [NSThread currentThread]);
// }
// });
//
// dispatch_async(concurrentQueue, ^{
// for (int i = 0; i < 10000; i++) {
// NSLog(@"任务3: %@", [NSThread currentThread]);
// }
// });
/*
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 子线程内处理耗时操作
NSString *str = @"http://s17.mogucdn.com/b2/pic/111217/k96n_kqyvmrlykrbew6cugfjeg5sckzsew_3000x2000.jpg";
NSURL *url = [NSURL URLWithString:str];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
// 回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
// 更新
_imageView.image = image;
});
});
*/
// 3.5.组
// 获取系统提供的并行队列
// dispatch_queue_t concurrentQueue2 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// // 创建一个组
// dispatch_group_t group = dispatch_group_create();
//
// 将任务,添加到group组中,并在concurrentQueue2队列中执行
// dispatch_group_async(group, concurrentQueue2, ^{
// for (int i = 0; i < 1; i++) {
// NSLog(@"任务1");
// }
// });
//
// dispatch_group_async(group, concurrentQueue2, ^{
// for (int i = 0; i < 10000; i++) {
// NSLog(@"任务2");
// }
// });
//
// dispatch_group_async(group, concurrentQueue2, ^{
// for (int i = 0; i < 8; i++) {
// NSLog(@"任务3");
// }
// });
//
// // 通知最后一个任务执行
// dispatch_group_notify(group, concurrentQueue2, ^{
// NSLog(@"最后的任务");
// });
// 3.6. 延迟时间执行
// dispathc_after.....
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"这里延迟5秒钟执行");
});
// 3.7. 只执行一次
// dispatch_once....
for (int i = 0; i < 10; i++) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"这里只执行一次");
});
}
// 3.8. 执行多次
// dispatch_apply(50, dispatch_get_global_queue(0, 0), ^(size_t t) {
// NSLog(@"执行多次");
//
//
// });
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"这里只执行一次");
});
// 耗时操作:网络请求、复杂的计算、数据库操作
}
- (void)test:(id)obj
{
for (int i = 0; i < 10000; i++) {
// NSLog(@"%@ %d", [NSThread currentThread], i);
NSLog(@"invocation");
}
}
- (void)loadImage
{
@autoreleasepool {
NSString *urlStr = @"http://s17.mogucdn.com/b2/pic/111217/k96n_kqyvmrlykrbew6cugfjeg5sckzsew_3000x2000.jpg";
NSURL *url = [NSURL URLWithString:urlStr];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
// 回到主线程更新页面
[self performSelectorOnMainThread:@selector(showImage:)
withObject:image
waitUntilDone:YES];
}
}
- (void)showImage:(UIImage *)image
{
NSLog(@"%@", [NSThread currentThread]);
_imageView.image = image;
}
- (void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
NSLog(@"%@", [NSThread currentThread]);
_operationQueue.maxConcurrentOperationCount = 2;
}