iOS 多线程/GCD

+ (SingleHandel *)shareModel

{

static dispatch_once_t onceQueue;

dispatch_once(&onceQueue, ^{

shareSingle = [[SingleHandel alloc] init];

});

return shareSingle;

}

/* //第一种线程开启方式

NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(threadAction) object:nil];

//给你的线程起个名字

thread1.name = @"thread1";

//启动线程

[thread1 start];

//关闭

[thread1 cancel];*/

//第二种线程开启方式(程序运行的时候自动开启线程)

/*[NSThread detachNewThreadSelector:@selector(threadAction) toTarget:self withObject:nil];*/

//第三种继承与NSObjet

// [self performSelectorInBackground:@selector(threadAction) withObject:nil];

 

 

// NSOperation

NSInvocationOperation *inOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(invocation) object:nil];

NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{

NSLog(@"我是block");

}];

// [blockOp start];

//创建队列

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

//设置最大同时执行数量

queue.maxConcurrentOperationCount = 1;

 

//添加事件

[queue addOperation:inOp];

// sleep(1);

[queue addOperation:blockOp];

 

}

 

- (void)invocation

{

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

NSLog(@"我是方法");

}

 

 

 

/-----------------------------------------------------GCD----------------------------------------------------------------/

 

多线程---GCD

- (IBAction)buttonAction:(id)sender

{

 

//GCD(先进先出first in first out 简称FIFO)

//串行 前一个执行完成之后,后一个任务才能执行

//并行 任务在派发时时有序的,不用等到第一个任务执行完成才开始下一个(也就是一块执行)

 

//GCD分为三种:主队列,全局队列,自定义队列

/*--------------------主队列(串行)----------------------*/

//使用主队列实现任务派发(串行),在主线程中

/*dispatch_queue_t mainQueue = dispatch_get_main_queue();

//添加任务

dispatch_async(mainQueue, ^{

NSLog(@"第一个任务%@",[NSThread currentThread]);

 

});

dispatch_async(mainQueue, ^{

NSLog(@"第二个任务:%@",[NSThread currentThread]);

});

dispatch_async(mainQueue, ^{

NSLog(@"第三个任务:%@",[NSThread currentThread]);

});*/

/*--------------------自定义队列(串行)----------------------*/

//自定义队列

/* dispatch_queue_t myQueue = dispatch_queue_create("com.myqueue", DISPATCH_QUEUE_SERIAL);

//添加任务

dispatch_async(myQueue, ^{

NSLog(@"第一个任务%@",[NSThread currentThread]);

});

dispatch_async(myQueue, ^{

sleep(3);

NSLog(@"第二个任务:%@",[NSThread currentThread]);

});

dispatch_async(myQueue, ^{

NSLog(@"第三个任务:%@",[NSThread currentThread]);

});*/

/*--------------------自定义队列(并行)----------------------*/

/* dispatch_queue_t myqueue1 = dispatch_queue_create("com.s", DISPATCH_QUEUE_CONCURRENT);

dispatch_async(myqueue1, ^{

NSLog(@"第一个任务%@",[NSThread currentThread]);

 

});

dispatch_async(myqueue1, ^{

//可以延时

sleep(3);

NSLog(@"第二个任务%@",[NSThread currentThread]);

 

 

});

dispatch_async(myqueue1, ^{

NSLog(@"第三个任务%@",[NSThread currentThread]);

 

});*/

 

/*--------------------全局队列(并行)----------------------*/

/* dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(globalQueue, ^{

sleep(2);

NSLog(@"第一个任务:%@",[NSThread currentThread]);

 

 

});

dispatch_async(globalQueue, ^{

NSLog(@"第二个任务:%@",[NSThread currentThread]);

 

});

dispatch_async(globalQueue, ^{

NSLog(@"第三个任务:%@",[NSThread currentThread]);

 

});*/

/*---------------------------------延时---------------------------------------------*/

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

[self afther];

});

 

/*-----------------------------重复执行---------------------------------------------*/

 

dispatch_apply(3, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t t) {

[self afther];

 

});

/*-----------------------------执行一次---------------------------------------------*/

static dispatch_once_t onceQueue ;

dispatch_once(&onceQueue, ^{

NSLog(@"我就执行一次");

});

 

/*----------------------------------线程通讯---------------------------------*/

//遇到线程通讯,执行他的方法,可以回到主线程(如果正在执行子线程,走这个方法可以跳出子线程到主线程)

[NSThread detachNewThreadSelector:@selector(newThread) toTarget:self withObject:nil ];

 

 

}

- (void)newThread

{

NSLog(@"我是子线程");

//回到主线程

[self performSelectorOnMainThread:@selector(afther) withObject:self waitUntilDone:NO];

}

-(void)afther

{

NSLog(@"sad");

}

posted @ 2016-05-25 16:12  ekhome  阅读(141)  评论(0编辑  收藏  举报