多线程举例

turboksiOS195

举例一:

- (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

相关文章

百度地图自定义marker

百度地图自定义marker

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

iOS的block

iOS的block

一:block的本质1.    block本质上是一个OC对象,它内部有个isa指针2.    block是封装了函数调用以及函数调用环境的OC对象3.&nbs...

iOS程序的内存布局

iOS程序的内存布局

    代码段:编译之后的代码    数据段:字符串常量:比如NSString * str = @“123”已初始化数据:已初始化的全局变量...

iOS 仿抖音打卡美好中国

iOS 仿抖音打卡美好中国

效果图这几天打卡中国挺火的,刚好有点时间就想着看看能不能模仿一下。Scrollview + CADisplayLink 设置背景UILongPressGestureRecognizer长按手...

OC对象的本质

OC对象的本质

我们平时编写的oc代码、底层实现其实是C\C++代码OC代码    ——>    C\C++    ——>  &nbs...

dealloc

dealloc

当调用dealloc方法的时候,执行顺序可以在源码中查找如下(objc源码):1.// Replaced by NSZombies- (void)dealloc {    _obj...