iOS 06-多线程NSThread的使用
2017-09-14 15:33
225 查看
1、创建一个按钮
- (IBAction)buttonClicked:(id)sender {
NSLog(@"main =%@",[NSThread
mainThread]);
// [self createThread1];
[self
createThread2];
// [self createThread3];
}
-(void)createThread3 {
//开启后台线程
[self
performSelectorInBackground:@selector(run:)
withObject:@"back"];
}
-(void)createThread2{
[NSThread
detachNewThreadSelector:@selector(run:)
toTarget:self
withObject:@"miss"];
}
-(void)createThread1{
//创建一个线程
NSThread *thread = [[NSThread
alloc]initWithTarget:self
selector:@selector(run:)
object:@"abc"];
thread.name =
@"myThread";
//启动线程
[thread start];
}
-(void)run:(id)obj {
NSLog(@"----run----%@--%@",[NSThread
currentThread], obj);
//线程休眠
//第一种方法
[NSThread
sleepForTimeInterval:3];
//第二种方法
// [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:3]];
//或者不再执行 distantFuture(遥远的未来)
// [NSThread sleepUntilDate:[NSDate distantFuture]];
//强制退出线程
for (int i =
0; i <1000000; i++) {
NSLog(@"--%d--",i);
if(i ==
100) {
//线程退出
[NSThread
exit];
}
}
}
- (IBAction)buttonClicked:(id)sender {
NSLog(@"main =%@",[NSThread
mainThread]);
// [self createThread1];
[self
createThread2];
// [self createThread3];
}
-(void)createThread3 {
//开启后台线程
[self
performSelectorInBackground:@selector(run:)
withObject:@"back"];
}
-(void)createThread2{
[NSThread
detachNewThreadSelector:@selector(run:)
toTarget:self
withObject:@"miss"];
}
-(void)createThread1{
//创建一个线程
NSThread *thread = [[NSThread
alloc]initWithTarget:self
selector:@selector(run:)
object:@"abc"];
thread.name =
@"myThread";
//启动线程
[thread start];
}
-(void)run:(id)obj {
NSLog(@"----run----%@--%@",[NSThread
currentThread], obj);
//线程休眠
//第一种方法
[NSThread
sleepForTimeInterval:3];
//第二种方法
// [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:3]];
//或者不再执行 distantFuture(遥远的未来)
// [NSThread sleepUntilDate:[NSDate distantFuture]];
//强制退出线程
for (int i =
0; i <1000000; i++) {
NSLog(@"--%d--",i);
if(i ==
100) {
//线程退出
[NSThread
exit];
}
}
}
相关文章推荐
- ios-day10-06(UIApplication的介绍。UIApplication的常用属性、使用UIApplication控制和管理状态栏)
- iOS学习- 06 UI设计 Sketch使用
- iOS开发:Swift多线程NSThread的使用
- ios-day05-06(灵活使用UIScrollView的三个属性:contentSize、contentInset、contentOffset,模仿“喜马拉雅”的界面)
- iOS学习笔记-146.网络06——NSURLConnection03_使用
- ios-day11-06(使用代码的方式创建导航控制器——UINavigationController、使用导航控制器管理其他控制器)
- iOS开发UI高级—06使用storyboard创建导航控制器以及控制器的生命周期
- 【iOS】数据库SQLite3的使用
- ios-day20-01(使用UIWebView实现一个简单功能的浏览器)
- iOS Delegate 的使用
- iOS定位原理和使用建议
- IOS中delegate的使用方法
- iOS开发之AFNetworking 3.0.4使用
- IOS相机使用学习笔记
- IOS 多线程NSThread、NSOperation、GCD详解
- iOS 使用AFNetworking遇到错误 Request failed: unacceptable content-type: text/html
- OD使用教程6 - 调试篇06|解密系列
- iOS - UIAlertController使用
- iOS中GCD的使用小结
- IOS开发之--异常处理--使用try 和 catch 来捕获错误。