iOS——NSThread
来源:互联网 发布:美工刀片自动装盒机 编辑:程序博客网 时间:2024/05/16 05:25
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ // [NSThread currentThread] 获得当前线程,在开发中经常打印。 所有多线程技术都能使用这个方法 // number == 1 主线程 // number != 1 其他线程,子线程, 次线程 NSLog(@"%s -- %@",__func__, [NSThread currentThread]); [NSThread currentThread];// [self longTimeOperation]; // 将耗时的操作放到子线程执行 // 会开辟一个子线程,并且在子线程执行longTimeOperation方法,后面传递参数// [self performSelectorOnMainThread:<#(nonnull SEL)#> withObject:<#(nullable id)#> waitUntilDone:<#(BOOL)#>]// [self performSelectorInBackground:<#(nonnull SEL)#> withObject:<#(nullable id)#>] [self performSelectorInBackground:@selector(longTimeOperation) withObject:nil]; // self performSelector:<#(nonnull SEL)#> withObject:<#(nullable id)#> afterDelay:<#(NSTimeInterval)#> NSLog(@"%@",[NSThread currentThread]);}#pragma mark - 耗时操作- (void)longTimeOperation{ for (int i = 0; i < 20000; i++) { NSLog(@"%d %@", i, [NSThread currentThread]); }}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self test3]; }// 耗时操作- (void)run:(NSString *)str{ for (int i = 0; i < 10; i++) { NSLog(@"%@--%d", [NSThread currentThread], i); }}#pragma mark - 线程的创建方式// 创建线程方式3- (void)test3{ // “隐式”创建线程方式 [self performSelectorInBackground:@selector(run:) withObject:@"cz"];}// 创建线程方式2- (void)test2{ NSLog(@"---%@", [NSThread currentThread]); [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"hello"]; NSLog(@"test2 --- %@", [NSThread currentThread]);}// 创建线程方式1- (void)test1{ // 实例化一个线程对像 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil]; // 让线程开始工作,启动线程, 在新开的线程执行run方法 [thread start]; }
#pragma mark - 线程的属性- (void)test4{ NSThread *threadA = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"hello"]; threadA.name = @"thraed A"; // 线程优先级 // 是一个浮点数,0.0~1.0。 默认值 0.5 // 开发的时候,一般不去修改优先级的值。 // 优先级,必须调用很多次的时候,才能体现出来。 threadA.threadPriority = 0.1; // 开始工作 [threadA start]; NSThread *threadB = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"hello"]; threadB.name = @"thraed B"; // 线程优先级 // 是一个浮点数,0.0~1.0。 默认值 0.5 threadB.threadPriority = 1.0; // 开始工作 [threadB start];}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self test];}- (void)test{ // 1. 新建一个线程 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil]; // 2. 放到可调度线程池,等待被调度。 这时候是就绪状态 [thread start];}- (void)run{ NSLog(@"%s", __func__); // 刚进来就睡会, 睡2秒// [NSThread sleepForTimeInterval:5.0]; // 睡到指定的时间点// [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:5.0]]; for (int i = 0; i < 20; i++) { // 满足某一个条件以后,阻塞线程的执行。 也就是让线程休息一会 if (i == 10) { [NSThread sleepForTimeInterval:3.0]; } // 一旦达到某一个条件,就强制终止线程的执行 if (i == 15) { // 一旦强制终止,就在不能重新启动 // 一旦强制终止,后面的代码都不会执行 [NSThread exit]; } NSLog(@"%@--- %d", [NSThread currentThread], i); } NSLog(@"线程结束");}
3 0
- iOS——NSThread
- ios学习路线—iOS高级(NSThread)
- IOS多线程之——NSThread
- iOS开发——多线程NSThread
- iOS多线程开发——NSThread浅析
- iOS开发—使用NSThread实现多线程
- iOS开发—iOS多线程编程之NSThread的使用
- iOS开发——多线程、NSThread、gcd、线程间通信
- iOS —— NSThread、NSOperation、GCD多线程的优缺点
- iOS多线程学习(2)——NSThread和NSOperation
- ios多线程操作(二)—— NSThread的应用
- iOS开发之多线程——NSThread、NSOperation、GCD
- ios多线程操作(二)—— NSThread的应用
- iOS多线程开发——NSThread的简单运用
- iOS之多线程——概念、pthread、NSThread、GCD
- IOS学习笔记23—多线程之NSThread、NSOperation、NSInvocationOperation
- IOS学习笔记23—多线程之NSThread、NSOperation、NSInvocationOperation
- IOS学习笔记23—多线程之NSThread、NSOperation、NSInvocationOperation
- ORA-00257处理方法
- git:Linux搭建服务器,windows客户端
- 网页性能之HTML、CSS、JS
- 5个JavaScript经典面试题
- Java学习之——线程非安全单利模式到线程安全单利模式
- iOS——NSThread
- Myeclipse2015 隐藏jar包
- 【TomoEngine】 第五讲 【MOBA制作日记一】 角色
- C++作业6
- Fragment全解析系列(二):正确的使用姿势
- Phpcms v9搜索结果不全少内容的解决方法
- Internal Sorting Algorithms Part 2/2: Advanced Sorts
- sed 正则表达式的'转义问题
- 23种设计模式(10)--Flyweight模式