#import "ViewController.h"
@interface ViewController ()
@property(nonatomic ,strong) NSThread *thread;
@end
@implementation ViewController
-(void)viewDidLoad
{
[super viewDidLoad];
//1.创建子线程
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(run2) object:nil];
self.thread = thread;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//2.启动
[self.thread start];
}
-(void)run
{
NSLog(@"%@",[NSThread currentThread]);
// [NSThread sleepForTimeInterval:2.0]; //阻塞线程2.0
// [NSThread sleepUntilDate:[NSDate distantFuture]];
// NSLog(@"%@",[NSDate distantFuture]); //4001-01-01 00:00:00 +0000
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:3.0]];
NSLog(@"-----end----");
}
-(void)run2
{
for (NSInteger i = 0; i<1; i++) {
NSLog(@"%zd---%@",i,[NSThread currentThread]);
if (i == 50) {
//强制线程退出
// [NSThread exit];
break;
}
}
}
@end