搜索
您的当前位置:首页正文

iOS队列小述

来源:二三娱乐

一:全局队列与并行队列的区别

dispatch_queue_t

q=dispatch_get_global_queue(DISOATCH_QUEUE_PRIORITY_DEFAULT,0);

1.不需要创建,直接get就能用

2.两个队列的执行效果相同

3.全局队列没有名称,调试时,无法确认准确队列

4.全局队列有高中默认优先级

二:并行队列

dispatch_queue_t q =

dispatch_queue_create("fixbird",DISPATCH_QUEUE_CONCURRENT);

三:串行队列

dispatch_queue_t t=

dispatch_queue_create("fixbird",DISPATCH_QUEUE_SERIAL):

四:开发中,跟中当前线程

[NSThread currentThread​]

五:并行队列的任务嵌套例子

dispatch_queue_t q =

dispatch_queue_create("fixbird",DISPATCH_QUEUE_CONCURRENT);

//任务嵌套

dispatch_sync(q, ^{

NSLog(@"1 %@", [NSThreadcurrentThread]);

dispatch_sync(q,^{

NSLog(@"2 %@", [NSThreadcurrentThread]);

dispatch_sync(q,^{

NSLog(@"3 %@", [NSThreadcurrentThread]);

});

});

dispatch_async(q,^{

NSLog(@"4 %@", [NSThreadcurrentThread]);

});

NSLog(@"5 %@", [NSThreadcurrentThread]);

});

运行结果:12345或12354

六:主队列(线程)

1每一个应用程序都有一个主线

2所有UI的更新工作,都必须在主线程上执行

3主线程一种工作,而且除非把程序杀掉,否则主线程的工作永不会结束!

dispatch_queue_t q = dispatch_get_main_queue();

七:在主队列上更新UI的例子

//创建代码块

void(^TaskOne)(void) =^(void)

{

NSLog(@"Current thread = %@", [NSThreadcurrentThread]);

NSLog(@"Main thread = %@", [NSThreadmainThread]);

[[[UIAlertView alloc]initWithTitle:@"GCD"

message:@"Great CenterDispatcher"

delegate:nil

cancelButtonTitle:@"OK"

otherButtonTitles:nil, nil] show];

};

//取得分发队列

dispatch_queue_t mainQueue =dispatch_get_main_queue();

//提交任务   dispatch_async(mainQueue,TaskOne);}

//简便写法  dispatch_async( dispatch_get_main_queue(),^(void)   {           NSLog(@"Current thread = %@", [NSThreadcurrentThread]);

NSLog(@"Main thread = %@", [NSThreadmainThread]);

[[[UIAlertView alloc]initWithTitle:@"GCD"

message:@"Great CenterDispatcher"

delegate:nil

cancelButtonTitle:@"OK"

otherButtonTitles:nil, nil]show];

});

//输出结果

//2014-05-02 20:34:27.872 serirl[835:60b] Current thread = {name

= (null), num = 1}

//2014-05-02 20:34:27.873 serirl[835:60b] Main thread =

{name = (null), num = 1}

​八:NSBlockOperation简单实用

//开发中一般给自定义队列定义为属性

@property (nonatomic, strong) NSOperationQueue

*myQueue;

self.myQueue = [[NSOperationQueue alloc] init];

1>在自定义队列

NSBlockOperation *block = [NSBlockOperationblockOperationWithBlock:^{

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

}];

所有的自定义队列,都是在子线程中运行.

[self.myQueueaddOperation:block];

或者:   [self.myQueue

addOperationWithBlock:^{

NSLog(@"%@", [NSThreadcurrentThread]);

}];

2>在主队列中执行

[[NSOperationQueue mainQueue] addOperationWithBlock:^{

NSLog(@"%@", [NSThreadcurrentThread]);

}];

//设定执行顺序,Dependency依赖,可能会开多个,单不会太多

//依赖关系是够可以跨队列的!

[op2 addDependency:op1];表示op2会在op1执行完之后才会执行

//GCD是串行队列,异步任务,只会开一个县城

[self.myQueue addOperation:op1];

[[NSOperationQueue mainQueue

]addOperation:op4]​;

九:NSInvocationOperation简单实用

NSInvocationOperation *op = [[NSInvocationOperation

alloc]initWtihTarget:self selector:@selector(demoOp:)

object:@"hello op"];

-(void)demoOp:(id)obj{

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

十:performSelectorOnMainThread方法实用

1模拟下载,延时

[NSThread sleepForTimeInterval:1.0];

2设置图像,苹果底层允许实用performSelectorInBackground方法

在后台县城更新UI,强烈建议大家不要这么做!!!

YES会阻塞住线程,直到调用方法完成

NO不会阻塞县城,会继续执行

[self

performSelectorOnMainThread:@selector(setImage:)

withObject:[UIImage imageNamed:imagePath]

waitUntilDone:NO];​

// 1. 图像

- (void)setImage:(UIImage

*)image

{

self.imageView.image =

image;

[self.imageView sizeToFit];

}

11.

提问:代码存在什么问题?如果循环次数非常大,会出现什么问题?应该如何修改?

//

解决办法1:如果i比较大,可以在for循环之后@autoreleasepool //

解决方法2:如果i玩命大,一次循环都会造成

自动释放池被填满,一次循环就@autoreleasepool

for (int i = 0; i < 10000000; ++i)

{

@autoreleasepool

{

//

*

NSString *str = @"Hello

World!";

// new

*

str = [str

uppercaseString];

// new

*

str = [NSString stringWithFormat:@"%@ %d", str,

i];

NSLog(@"%@",

str);

}    }

Top