iOS多线程技术的深度探究一: NSObject多线程技术

/*
 NSObject多线程方法
 1. [NSThread currentThread] 可以返回当前运行的线程
    返回的NSThred 中Number等于1表示为主线程
 在任何多线程技术中(NSThread,NSOperation,GCD),都可以使用此方法来查看当前线程的情况
 2. performSelectorInBackground 新建后台线程
    此方法可以修改界面,但强烈建议不要如此使用
 3. 更新界面
    使用performSelectorOnMainThread可以在主线程中执行任务
 
    温馨提示:NSObject对象均可调用此方法
 4. 内存管理
    多线程任务一定要包含在@autoreleasepool(自动释放池)中,否则会引起内存泄露,并且很难发现
 5. 线程休眠(使用与开发调试使用)
    [NSThread sleepForTimeInterval:1.0f]
 
 特点
 1. 使用简单,量级亲
 2. 不能控制线程的执行顺序
 */


#import "ViewController.h"


@interface ViewController ()
{
    UIView *view;
}
@end
@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button1 setTitle:@"BigTask" forState:UIControlStateNormal];
//    [button1 setBackgroundColor:[UIColor blackColor]];
    button1.frame = CGRectMake(100, 100, 100, 40);
    [self.view addSubview:button1];
    [button1 addTarget:self action:@selector(didButton1Clicked) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button2 setTitle:@"BigTask" forState:UIControlStateNormal];
//    [button1 setBackgroundColor:[UIColor blackColor]];
    button2.frame = CGRectMake(100, 200, 100, 40);
    [self.view addSubview:button2];
    [button2 addTarget:self action:@selector(didButton2Clicked) forControlEvents:UIControlEventTouchUpInside];
    
    view = [[UIView alloc] initWithFrame:CGRectMake(100, 260, 100, 100)];
    view.backgroundColor = [UIColor redColor];
    [self.view addSubview:view];
    
    NSLog(@"主线程:%@",[NSThread currentThread]);
}


- (void)didButton1Clicked
{
    //在后台调用耗时操作,performSelectorInBackground会新建一个后台线程,并在该线程中执行这个方法
    [self performSelectorInBackground:@selector(bigTask:) withObject:[NSNumber numberWithInt:1000]];
}


- (void)didButton2Clicked
{
    [self smallTask:[NSNumber numberWithInt:1000]];
//    [self performSelectorInBackground:@selector(smallTask:) withObject:[NSNumber numberWithInt:1000]];
}


- (void)bigTask:(NSNumber *)i
{
    @autoreleasepool {
        for (int j = 0; j<[i intValue]; j++) {
            NSString *str = [NSString stringWithFormat:@"%i",j];
            NSLog(@"%@",str);
        }
        view.backgroundColor = [UIColor greenColor];
        //在主线程中修改UI
        [self performSelectorOnMainThread:@selector(setUI) withObject:nil waitUntilDone:YES];
        NSLog(@"大任务线程:%@",[NSThread currentThread]);
    }
}


- (void)setUI
{
    view.backgroundColor = [UIColor yellowColor];
}
- (void)smallTask:(NSNumber *)i
{
    NSString *str = nil;
    for (int j = 0; j<[i intValue]; j++) {
        str = [NSString stringWithFormat:@"%i",j];
    }
    NSLog(@"%@",str);
    view.backgroundColor = [UIColor redColor];
    NSLog(@"小任务线程:%@",[NSThread currentThread]);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

你可能感兴趣的:(iOS多线程专辑)