多线程举例

turboksiOS251

举例一:

- (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的分类(Category)

iOS的分类(Category)

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

CPU和GPU

CPU和GPU

    在屏幕成像的过程中,CPU和GPU起着至关重要的作用:CPU(Central Processing Unit,中央处理器)对象的创建和销毁、对象属性的调...

GCD定时器

GCD定时器

NSTimer依赖于RunLoop,如果RunLoop的任务过于繁重,可能会导致NSTimer不准时。而GCD的定时器会更加准时。- (void)viewDidLoad {   ...

百度地图自定义marker

百度地图自定义marker

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

iOS短视频开发之---AVPlayer

iOS短视频开发之---AVPlayer

基于原生的AVPlayer + ScrollView实现,包含视频播放、暂停、点赞动画实现。视频是采用的本地视频、需要网络视频请自行添加添加测试。添加方式:  //本地视频 &n...

iOS多线程

iOS多线程

多线程常见方案一、GCD的函数:GCD中有2个用来执行任务的函数queue:队列block:任务1.    用同步的方式执行任务dispatch_sync(di...