首页 > 系统 > iOS > 正文

iOS中NSThread线程

2019-11-06 10:00:25
字体:
来源:转载
供稿:网友

#import "NSThreadViewController.h"

@interface NSThreadViewController ()

@end

@implementation NSThreadViewController

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. }

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

[self threadDemo];

}

-(void)threadDemo{

NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(demo:) object:@"ddd"];// detach 分离 出来一个子线程[NSThread detachNewThreadSelector:@selector(demo:) toTarget:self withObject:@"tttt"];[thread start];//子线程 所有继承NSObject的都可以使用[self performSelectorInBackground:@selector(demo:) withObject:@"ddd"];//在指定线程上进行操作[self performSelector:@selector(demo:) onThread:[NSThread mainThread] withObject:@"4444" waitUntilDone:NO];

}

-(void)demo:(id)obj {

//交替执行线程for ( int i = 0; i< 2; i++) { NSLog(@"%@%@",[NSThread currentThread],obj);}

}

@end


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表