多线程THread

来源:互联网 发布:柳天捏脸数据 编辑:程序博客网 时间:2024/05/19 20:44

文件1:ViewController.m

#import "ViewController.h"
#import "MyThread.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.label.backgroundColor = [UIColor blackColor];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void) print
{
    NSArray *arr = [NSThread callStackSymbols];// 线程函数地址
    NSLog(@"%@",arr);
    
    
    for (int i = 0 ; i<200; i++) {
        NSLog(@"%i",i);
        [self.label setText:[NSString stringWithFormat:@"%d",i]];
        
        //不能在子线程中刷新UI
        
        //[self.label performSelectorOnMainThread:@selector(setText:) withObject:[NSString stringWithFormat:@"%d",i] waitUntilDone:YES];
        [self performSelectorOnMainThread:@selector(labelText:) withObject:[NSString stringWithFormat:@"%i",i] waitUntilDone:YES];
        
        NSLog(@"%@",[NSThread isMainThread]?@"main":@"sub");// 判断当前线程是否为主线程
        if (i>100) {
            NSThread *thread = [NSThread currentThread];//返回当前线程
            [thread cancel];// 取消操作
            NSLog(@"%i",thread.isCancelled);// 是否取消的
        }
        
        
        //使所在的线程暂停
        [NSThread sleepForTimeInterval:1.0f];
        [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0f]];
    }
}


- (void) labelText:(NSNumber *)value
{
    self.label.text = [NSString stringWithFormat:@"%@",value];
    self.label.textColor = [UIColor whiteColor];
//    if ([value intValue] % 2 ) {
//        _label.backgroundColor = [UIColor redColor];
//    }
//    else {
//        _label.backgroundColor = [UIColor greenColor];
//    }
    self.label.backgroundColor = [UIColor colorWithRed:(int)value/2000 green:1 blue:0 alpha:1];
    self.label.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1-(int)value/2000];

//退出线程
//    [NSThread exit];
    
//退出程序
//    exit(0);
//    abort();
}
- (IBAction)didClicked:(id)sender {
    NSLog(@"sldskdf");
//    [self print];
    
    //创建子线程的几种方式:
    [self performSelectorInBackground:@selector(print) withObject:nil];
//    [NSThread detachNewThreadSelector:@selector(print) toTarget:self withObject:nil];
//    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(print) object:nil];
//    [thread start];
//    MyThread *myThread = [[MyThread alloc] init];
//    myThread.label = self.label;
//    [myThread start];
}

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

@end



文件2:MyThread.h

#import <UIKit/UIKit.h>

@interface MyThread : NSThread
@property(nonatomic,weak) UILabel *label;
@end

文件3:MyThread.m

#import "MyThread.h"

@implementation MyThread
//线程入口
- (void) main
{
    [self print];
}
- (void) print
{
    for (int i = 0; i<100; i++) {
        NSLog(@"%i",i);
        [_label performSelectorOnMainThread:@selector(setText:) withObject:[NSString stringWithFormat:@"%i",i] waitUntilDone:YES];
        if (i/2==0) {
            [_label performSelectorOnMainThread:@selector(setBackgroundColor:) withObject:[UIColor whiteColor] waitUntilDone:YES];
        }
        else
        {
            [_label performSelectorOnMainThread:@selector(setBackgroundColor:) withObject:[UIColor redColor] waitUntilDone:YES];
        }
    }
}
@end

0 0