多线程-GCD1
2015-11-23 21:45
225 查看
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//iOS里面共有四种实现多线程的方式
//1、NSObject 自带了performselectoronbackground(最简单最轻量级的级别)(它是隐式的创建线程)NSThead;
//2、NSThread
//1>、第一种创建方式
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(jisuanNumber) object:nil];
//这种创建的NSThread方式必须手动开启线程
[thread start];//必须开启
//1、获取当前线程
[NSThread currentThread];
//2、判断一个线程是否是主线程
[NSThread isMainThread];
NSLog(@"viewDidLoad:%@,%d",[NSThread currentThread],[NSThread isMainThread]);
//2>、第二种创建方式
[NSThread detachNewThreadSelector:@selector(jisuanNumber) toTarget:self withObject:nil];//自动开启任务
}
#pragma mark 计算ButtonDidCkicked(单线程)
- (IBAction)jisuanButtonDidClicked:(id)sender {
//1、NSObject
[self performSelectorInBackground:@selector(jisuanNumber) withObject:nil];
}
- (void)jisuanNumber {
@autoreleasepool {//必须有自动释放池
NSLog(@"%@,%d",[NSThread currentThread],[NSThread isMainThread]);
NSInteger sum = 0;
for (NSInteger i = 1; i < 1000000000; i++) {
sum += i;
}
// NSLog(@"%ld",(long)sum);
[self performSelectorOnMainThread:@selector(result:) withObject:@(sum) waitUntilDone:YES];
}
}
- (void)result:(NSNumber *)sum {
NSLog(@"%ld",(long)sum);
}
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//iOS里面共有四种实现多线程的方式
//1、NSObject 自带了performselectoronbackground(最简单最轻量级的级别)(它是隐式的创建线程)NSThead;
//2、NSThread
//1>、第一种创建方式
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(jisuanNumber) object:nil];
//这种创建的NSThread方式必须手动开启线程
[thread start];//必须开启
//1、获取当前线程
[NSThread currentThread];
//2、判断一个线程是否是主线程
[NSThread isMainThread];
NSLog(@"viewDidLoad:%@,%d",[NSThread currentThread],[NSThread isMainThread]);
//2>、第二种创建方式
[NSThread detachNewThreadSelector:@selector(jisuanNumber) toTarget:self withObject:nil];//自动开启任务
}
#pragma mark 计算ButtonDidCkicked(单线程)
- (IBAction)jisuanButtonDidClicked:(id)sender {
//1、NSObject
[self performSelectorInBackground:@selector(jisuanNumber) withObject:nil];
}
- (void)jisuanNumber {
@autoreleasepool {//必须有自动释放池
NSLog(@"%@,%d",[NSThread currentThread],[NSThread isMainThread]);
NSInteger sum = 0;
for (NSInteger i = 1; i < 1000000000; i++) {
sum += i;
}
// NSLog(@"%ld",(long)sum);
[self performSelectorOnMainThread:@selector(result:) withObject:@(sum) waitUntilDone:YES];
}
}
- (void)result:(NSNumber *)sum {
NSLog(@"%ld",(long)sum);
}
相关文章推荐
- angularJS学习之路(十八)---自定义指令执行过程
- 黑马程序员——Map
- vim编辑器
- TextView中ellipsize属性
- I2C和SPI学习资源整理
- java基础入门-常见的DOS命令
- Android DiskLruCache缓存完全解析
- UVa 10574 - Counting Rectangles
- 再不写,我怕就再也不写了-LAMP基础
- 芯片设计流程概述
- ng-show ng-disabled ng-show ng-hide 值的布尔值改变时,状态发生变化
- 建立单向链表
- css方法div固定在网页底部
- 【应用笔记】【AN003】VC++环境下基于以太网的4-20mA电流采集
- [置顶] 泛型--前世今生
- 二叉树的基本操作的c++实现
- Linux默认配置文件系统路径 文件包含漏洞
- C++ for_each学习笔记
- iOS中的Quartz 2D
- Bootstrap图片支持响应式