iOS——NSThread
2016-05-19 09:48
344 查看
- (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(@"线程结束"); }
相关文章推荐
- iOS开发小技巧--实现将图片保存到本地相册
- iOS 文件夹不作备份处理
- iOS基于MVC的项目重构总结
- 8行代码教你搞定iOS导航控制器全屏滑动返回效果
- iOS跳转麦克风
- iOS学习之—— NSUserDefaults
- iOS学习之——NSSet、NSMutableSet
- iOS微博项目(六)实现左滑,大图,用户资料页
- 【代码笔记】iOS-把<br!>换成\n
- IOS 整体框架类图
- [绍棠] ios常见问题解决
- iOS Keychain (钥匙串)简单封装
- IOS json解析的四种方式
- iOS动画篇:自定义动画
- iOS各种证书配置总结
- OC 解析RGB 以及十六进制颜色的宏定义
- iOS7 定位服务未开启的提示
- iOS开发-Xcode Debug、Release、Archive、Profile、Analyze
- iOS-控制台的警告
- 关于iOS手势