多线程-GCD1
来源:互联网 发布:协同过滤算法基于物品 编辑:程序博客网 时间:2024/04/29 11:17
#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);
}
0 0
- 多线程-GCD1
- GCD1
- GCD1
- 多线程与GCD1:6种方法开启多线程
- 【莫比乌斯反演】GCD1
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 如何在oracle数据库里面建立多个用户连接到plsql客户端
- ORA-01950: 表空间'USERS'中无权限的2种解决办法
- 获取客户端IP地址(多层代理)
- STDIN_FILENO与stdin 的区别
- 具体的压栈指令,例子
- 多线程-GCD1
- Linux默认配置文件系统路径 文件包含漏洞
- iOS中的Quartz 2D
- 二叉树的基本操作的c++实现
- 泛型--前世今生
- IP控件
- autocad二次开发资料总结
- java基础入门-常见的DOS命令
- Android DiskLruCache缓存完全解析