iOS——NSThread

来源:互联网 发布:美工刀片自动装盒机 编辑:程序博客网 时间:2024/05/16 05:25
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    // [NSThread currentThread] 获得当前线程,在开发中经常打印。 所有多线程技术都能使用这个方法    // number == 1 主线程    // number != 1 其他线程,子线程, 次线程    NSLog(@"%s -- %@",__func__, [NSThread currentThread]);    [NSThread currentThread];//    [self longTimeOperation];    // 将耗时的操作放到子线程执行    // 会开辟一个子线程,并且在子线程执行longTimeOperation方法,后面传递参数//    [self performSelectorOnMainThread:<#(nonnull SEL)#> withObject:<#(nullable id)#> waitUntilDone:<#(BOOL)#>]//    [self performSelectorInBackground:<#(nonnull SEL)#> withObject:<#(nullable id)#>]    [self performSelectorInBackground:@selector(longTimeOperation) withObject:nil];    //    self performSelector:<#(nonnull SEL)#> withObject:<#(nullable id)#> afterDelay:<#(NSTimeInterval)#>    NSLog(@"%@",[NSThread currentThread]);}#pragma mark - 耗时操作- (void)longTimeOperation{    for (int i = 0; i < 20000; i++) {        NSLog(@"%d %@", i, [NSThread currentThread]);    }}



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    [self test3];    }// 耗时操作- (void)run:(NSString *)str{    for (int i = 0; i < 10; i++) {        NSLog(@"%@--%d", [NSThread currentThread], i);    }}#pragma mark  - 线程的创建方式// 创建线程方式3- (void)test3{    // “隐式”创建线程方式    [self performSelectorInBackground:@selector(run:) withObject:@"cz"];}// 创建线程方式2- (void)test2{    NSLog(@"---%@", [NSThread currentThread]);    [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"hello"];        NSLog(@"test2 --- %@", [NSThread currentThread]);}// 创建线程方式1- (void)test1{    // 实例化一个线程对像    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];        // 让线程开始工作,启动线程, 在新开的线程执行run方法    [thread start];    }

#pragma mark - 线程的属性- (void)test4{    NSThread *threadA = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"hello"];        threadA.name = @"thraed A";        // 线程优先级    // 是一个浮点数,0.0~1.0。 默认值 0.5    // 开发的时候,一般不去修改优先级的值。    // 优先级,必须调用很多次的时候,才能体现出来。    threadA.threadPriority = 0.1;        // 开始工作    [threadA start];            NSThread *threadB = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"hello"];        threadB.name = @"thraed B";        // 线程优先级    // 是一个浮点数,0.0~1.0。 默认值 0.5    threadB.threadPriority = 1.0;        // 开始工作    [threadB start];}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    [self test];}- (void)test{    // 1. 新建一个线程    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];        // 2. 放到可调度线程池,等待被调度。 这时候是就绪状态    [thread start];}- (void)run{    NSLog(@"%s", __func__);        // 刚进来就睡会, 睡2秒//    [NSThread sleepForTimeInterval:5.0];        // 睡到指定的时间点//    [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:5.0]];        for (int i = 0; i < 20; i++) {                // 满足某一个条件以后,阻塞线程的执行。 也就是让线程休息一会        if (i == 10) {            [NSThread sleepForTimeInterval:3.0];        }                // 一旦达到某一个条件,就强制终止线程的执行        if (i == 15) {            // 一旦强制终止,就在不能重新启动            // 一旦强制终止,后面的代码都不会执行            [NSThread exit];        }                NSLog(@"%@--- %d", [NSThread currentThread], i);    }        NSLog(@"线程结束");}



3 0
原创粉丝点击