常驻线程
- (void)viewDidLoad {
[super viewDidLoad];
self.thread = [[XMGThread alloc] initWithTarget:self selector:@selector(run) object:nil];
[self.thread start];
}
- (void)run
{
NSLog(@"----------run----%@", [NSThread currentThread]);
while (1);
NSLog(@"---------");
}
flag = 1
- (void)run
{
NSLog(@"----------run----%@", [NSThread currentThread]);
while (flag) {
[[NSRunLoop currentRunLoop] run];
NSLog(@"----4444-----");
}
NSLog(@"---------");
}
- (IBAction)btn:(id)sender {
flag = 0;
}
- (void)run
{
NSLog(@"----------run----%@", [NSThread currentThread]);
[[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
NSLog(@"---------");
}
- (IBAction)btn:(id)sender {
[self performSelector:@selector(test1) onThread:self.thread withObject:nil waitUntilDone:NO];
}
-(void)test1{
[NSThread exit];
}
- (IBAction)btn:(id)sender {
[self performSelector:@selector(stop) onThread:self.thread withObject:nil waitUntilDone:NO];
}
- (void)stop
{
[self performSelector:@selector(_stop) onThread:_thread withObject:nil waitUntilDone:NO];
_thread = nil;
}
- (void)_stop
{
CFRunLoopStop(CFRunLoopGetCurrent());
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self performSelector:@selector(test) onThread:self.thread withObject:nil waitUntilDone:NO];
}
- (void)test
{
NSLog(@"----------test----%@", [NSThread currentThread]);
}