iOS学习笔记31-多线程深入

  /**

     *创建一个队列(串行)

     **/

    dispatch_queue_t queue1 = dispatch_queue_create("queue1", DISPATCH_QUEUE_SERIAL);

    

    /**

     *添加代码块 执行线程的路径

     **/

    dispatch_async(queue1, ^{

        for (int i = 0; i<100; i++) {

            NSLog(@"线程一,%d",i);

        }

    });

 

 

 

 

用C语言函数的方式创建线程

    dispatch_sync_f(queue1, NULL, treadFun);//用线程函数的方式作为线程的路径

 

 

 

void treadFun(){

    for (int i =0 ; i<100; i++) {

        NSLog(@"线程2,%d",i);

    }

 

}

 

 

//    dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 2*NSEC_PER_SEC);

//    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

//    dispatch_after(time, queue, ^{

//        NSLog(@"222");

//    });

 

 

上面代码等价于

 NSLog(@"11111");

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        NSLog(@"2222222");

    });

 

 

 

/**

 *过一段时间后才执行

 1,dispatch_after

 2,NSObject performSelector

 3,timer 定时器方法

 **/

 

 

 

 [super viewDidLoad];

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(myfun) userInfo:nil repeats:YES];

    

    [timer invalidate];

 

 

 

//    [self performSelector:@selector(myfun) withObject:nil afterDelay:5.0];

 

posted @ 2016-02-29 10:21  small-elephant_A-Do  阅读(116)  评论(0编辑  收藏  举报