多线程举例

turboksiOS58

举例一:

- (void)viewDidLoad {

    [super viewDidLoad];

    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

       dispatch_async(queue, ^{

           NSLog(@"1---%@",[NSThread currentThread]);

           [self performSelector:@selector(test) withObject:nil afterDelay:.0f];

           NSLog(@"3---%@",[NSThread currentThread]);

       });

}

-(void)test{

    NSLog(@"%s",__func__);

}

 ProjectTwo[2606:77536] 1---<NSThread: 0x600001b68380>{number = 7, name = (null)}

 ProjectTwo[2606:77536] 3---<NSThread: 0x600001b68380>{number = 7, name = (null)}

举例二:

- (void)viewDidLoad {

    [super viewDidLoad];

    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

    dispatch_sync(queue, ^{

        NSLog(@"1---%@",[NSThread currentThread]);

        [self performSelector:@selector(test) withObject:nil afterDelay:.0f];

        NSLog(@"3---%@",[NSThread currentThread]);

    });

}

-(void)test{

    NSLog(@"%s",__func__);

}

 ProjectTwo[2697:81240] 1---<NSThread: 0x600003858880>{number = 1, name = main}

 ProjectTwo[2697:81240] 3---<NSThread: 0x600003858880>{number = 1, name = main}

 ProjectTwo[2697:81240] -[ViewController test]

举例三:

- (void)viewDidLoad {

    [super viewDidLoad];

    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

    dispatch_async(queue, ^{

        NSLog(@"1---%@",[NSThread currentThread]);

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

        NSLog(@"3---%@",[NSThread currentThread]);

    });

}

-(void)test{

    NSLog(@"%s",__func__);

}

 ProjectTwo[2724:82458] 1---<NSThread: 0x600001f47cc0>{number = 7, name = (null)}

 ProjectTwo[2724:82458] -[ViewController test]

 ProjectTwo[2724:82458] 3---<NSThread: 0x600001f47cc0>{number = 7, name = (null)}

举例四:

- (void)viewDidLoad {

    [super viewDidLoad];

    NSThread *thread = [[NSThread alloc] initWithBlock:^{

       NSLog(@"1---%@",[NSThread currentThread]);

    }];

    [threadstart];

    [self performSelector:@selector(test) onThread:thread withObject:nil waitUntilDone:YES];

}

-(void)test{

    NSLog(@"%s",__func__);

}

 ProjectTwo[2765:84753] 1---<NSThread: 0x600001031300>{number = 7, name = (null)}

 ProjectTwo[2765:84554] *** Terminating app due to uncaught exception 'NSDestinationInvalidException', reason: '*** -[ViewController performSelector:onThread:withObject:waitUntilDone:modes:]: target thread exited while waiting for the perform'

举例五:

- (void)viewDidLoad {

    [super viewDidLoad];

    NSLog(@"执行任务1--%@",[NSThreadcurrentThread]);

    dispatch_queue_t queue = dispatch_get_main_queue();

    dispatch_sync(queue, ^{

        NSLog(@"执行任务2--%@",[NSThreadcurrentThread]);

    });

    NSLog(@"执行任务3--%@",[NSThreadcurrentThread]);

}

 ProjectTwo[2785:85589] 执行任务1--<NSThread: 0x6000009ec900>{number = 1, name = main}

(lldb) 

举例六:

- (void)viewDidLoad {

    [super viewDidLoad];

    NSLog(@"执行任务1--%@",[NSThreadcurrentThread]);

    dispatch_queue_t queue = dispatch_get_main_queue();

    dispatch_async(queue, ^{

        NSLog(@"执行任务2--%@",[NSThreadcurrentThread]);

    });

    NSLog(@"执行任务3--%@",[NSThreadcurrentThread]);

}

 ProjectTwo[2825:87240] 执行任务1--<NSThread: 0x6000014d0240>{number = 1, name = main}

 ProjectTwo[2825:87240] 执行任务3--<NSThread: 0x6000014d0240>{number = 1, name = main}

 ProjectTwo[2825:87240] 执行任务2--<NSThread: 0x6000014d0240>{number = 1, name = main}

举例七:

- (void)viewDidLoad {

    [super viewDidLoad];

    NSLog(@"执行任务1--%@",[NSThreadcurrentThread]);

    dispatch_queue_t queue = dispatch_queue_create("myqueu", DISPATCH_QUEUE_SERIAL);

    dispatch_async(queue, ^{

        NSLog(@"执行任务2--%@",[NSThreadcurrentThread]);

        dispatch_sync(queue, ^{

            NSLog(@"执行任务3--%@",[NSThreadcurrentThread]);

        });

        NSLog(@"执行任务4--%@",[NSThreadcurrentThread]);

    });

    NSLog(@"执行任务5--%@",[NSThreadcurrentThread]);

}

 ProjectTwo[2845:88258] 执行任务1--<NSThread: 0x600002dac700>{number = 1, name = main}

 ProjectTwo[2845:88347] 执行任务2--<NSThread: 0x600002de11c0>{number = 4, name = (null)}

 ProjectTwo[2845:88258] 执行任务5--<NSThread: 0x600002dac700>{number = 1, name = main}

(lldb) 

举例八:

- (void)viewDidLoad {

    [super viewDidLoad];

    NSLog(@"执行任务1--%@",[NSThreadcurrentThread]);

    dispatch_queue_t queue = dispatch_queue_create("myqueu", DISPATCH_QUEUE_SERIAL);

    dispatch_queue_t queue2 = dispatch_queue_create("myqueu2", DISPATCH_QUEUE_SERIAL);

    dispatch_async(queue, ^{

        NSLog(@"执行任务2--%@",[NSThreadcurrentThread]);

        dispatch_sync(queue2, ^{

            NSLog(@"执行任务3--%@",[NSThreadcurrentThread]);

        });

        NSLog(@"执行任务4--%@",[NSThreadcurrentThread]);

    });

    NSLog(@"执行任务5--%@",[NSThreadcurrentThread]);

}

 ProjectTwo[2867:89315] 执行任务1--<NSThread: 0x600001310480>{number = 1, name = main}

 ProjectTwo[2867:89417] 执行任务2--<NSThread: 0x600001350d80>{number = 4, name = (null)}

 ProjectTwo[2867:89315] 执行任务5--<NSThread: 0x600001310480>{number = 1, name = main}

 ProjectTwo[2867:89417] 执行任务3--<NSThread: 0x600001350d80>{number = 4, name = (null)}

 ProjectTwo[2867:89417] 执行任务4--<NSThread: 0x600001350d80>{number = 4, name = (null)}

举例九:

- (void)viewDidLoad {

    [super viewDidLoad];

    NSLog(@"执行任务1--%@",[NSThreadcurrentThread]);

    dispatch_queue_t queue = dispatch_queue_create("myqueu", DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(queue, ^{

        NSLog(@"执行任务2--%@",[NSThreadcurrentThread]);

        dispatch_sync(queue, ^{

            NSLog(@"执行任务3--%@",[NSThreadcurrentThread]);

        });

        NSLog(@"执行任务4--%@",[NSThreadcurrentThread]);

    });

    NSLog(@"执行任务5--%@",[NSThreadcurrentThread]);

}

 ProjectTwo[2891:90412] 执行任务1--<NSThread: 0x600002420100>{number = 1, name = main}

 ProjectTwo[2891:90412] 执行任务5--<NSThread: 0x600002420100>{number = 1, name = main}

 ProjectTwo[2891:90486] 执行任务2--<NSThread: 0x600002462580>{number = 7, name = (null)}

 ProjectTwo[2891:90486] 执行任务3--<NSThread: 0x600002462580>{number = 7, name = (null)}

 ProjectTwo[2891:90486] 执行任务4--<NSThread: 0x600002462580>{number = 7, name = (null)}



返回列表

上一篇:iOS多线程

下一篇:RunLoop

相关文章

iOS图片圆角设置

iOS图片圆角设置

    UIImageView * vv = [[UIImageView alloc] initWithFrame:CGRectMake(50, 500, 250, 25...

百度地图自定义marker

百度地图自定义marker

#import "HomeViewController.h"#import "PaopaoView.h"@interface HomeViewControlle...

iOS 之导航渐变,并且跳转返回保持滑动状态

iOS 之导航渐变,并且跳转返回保持滑动状态

导航刚开始进入为透明,根据我们的滑动确定要显示的背景色。先看一下效果图:一:创建demo,将3个界面添加到控制器中,此过程比较简单代码就不上了。二:在第一页的viewDidload中将当前页面的导航设...

iOS的分类(Category)

iOS的分类(Category)

一:Category的底层结构定义在objc-runtime-new.h中:struct category_t {    const char *name;   ...

iOS的KVO、KVC

iOS的KVO、KVC

一:KVO  在实际的开发过程中,我们可能会遇到这种需求,就是需要根据一个对象某个属性值的改变,来作出不同的处理。但是实现起来比较麻烦,需要在很多地方进行判断处理,为了方便我们的开...

iOS一行代码搞定自定义AlertView

iOS一行代码搞定自定义AlertView

效果图走一走:使用方式: pod  'KSAlertView'举例使用,单按钮Alert: [[[KSAlertView alloc] initWithT...