NSThread

简介:


#import "ViewController.h"

 

@interface ViewController ()

{

    NSThread *thread1;

    NSThread *thread2;

    UIButton *btn;

 

}

 @end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

     btn=[UIButton buttonWithType:UIButtonTypeCustom];

    btn.frame=CGRectMake(30, 30, 50, 50);

    [btn setTitle:@"按钮" forState:UIControlStateNormal];

    btn.backgroundColor=[UIColor greenColor];

    [self.view addSubview:btn];

    //线程创建 主要有两种方法

     thread1=[[NSThread alloc] initWithTarget:self selector:@selector(threadfun1) object:nil];

     [thread1 start];//此方法创建需要手动启动

    //此方法自动启动线程方法

     [NSThread detachNewThreadSelector:@selector(threadfun2) toTarget:self withObject:nil];

 

}

-(void)threadfun1

{

    NSLog(@"thread1");

    //调用主线程更新

    [self performSelectorOnMainThread:@selector(upbutton) withObject:nil waitUntilDone:NO];

    

}

-(void)upbutton

{

    [btn setTitle:@"123" forState:UIControlStateNormal];

    [self performSelector:@selector(threadtothread) withObject:nil];

     

}

-(void)threadtothread

{

    NSLog(@"threadtothread");

}

-(void)threadfun2

{

    NSLog(@"thread2");

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end 

相关文章
|
4月前
GCD与NSThread的区别
GCD与NSThread的区别
218 65
|
8月前
|
消息中间件 Java Android开发
Handler HandlerThread 以及Thread
Handler HandlerThread 以及Thread
39 1
|
Java
线程中断方法interrupt、isInterrupted、interrupted方法
线程中断方法interrupt、isInterrupted、interrupted方法
130 0
线程中断方法interrupt、isInterrupted、interrupted方法
|
Java API
interrupted()和isInterrupted()
interrupted()和isInterrupted()
162 0
|
安全
NSThread
创建和启动线程 一个 NSThread 对象就代表一条线程 创建、启动线程 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:...
896 0