- 浏览: 1214456 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
lankk:
lankk 写道事实上,在运行String s1=new St ...
理解String 及 String.intern() 在实际中的应用 -
lankk:
事实上,在运行String s1=new String(&qu ...
理解String 及 String.intern() 在实际中的应用 -
lankk:
同意1楼的说法http://docs.oracle.com/j ...
理解String 及 String.intern() 在实际中的应用 -
raoyutao:
...
jdk 线程池 ThreadPoolExecutor -
hongdanning:
理解了。之前困惑的一些明白了。谢谢分享。
理解String 及 String.intern() 在实际中的应用
https://www.evernote.com/shard/s20/sh/54fbd735-53b6-4436-9bd4-88d46b628527/22bc221c73b55dbf1ddfb5515649c239
1. GCD 使用后不用程序去管理线程的开闭,GCD会在系统层面上去动态检测系统状态,开闭线程
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
for (__block int i = 0; i<10000; i++) {
dispatch_async(aQueue, ^{
NSLog(@"%d",i);
});
}2011-07-05 17:11:38.346 ttt[41418:1803] 61
2011-07-05 17:11:38.346 ttt[41418:5f03] 1292
2011-07-05 17:11:38.348 ttt[41418:1803] 4096
2011-07-05 17:11:38.348 ttt[41418:5f03] 4954
2011-07-05 17:11:38.349 ttt[41418:1803] 5823
2011-07-05 17:11:38.349 ttt[41418:5f03] 6159
2011-07-05 17:11:38.349 ttt[41418:1803] 6575
2011-07-05 17:11:38.349 ttt[41418:5f03] 6634
2011-07-05 17:11:38.350 ttt[41418:1803] 7936
2011-07-05 17:11:38.350 ttt[41418:5f03] 8428
2011-07-05 17:11:38.351 ttt[41418:1803] 8895
2011-07-05 17:11:38.351 ttt[41418:5f03] 9364
2011-07-05 17:11:38.351 ttt[41418:1803] 9836
2011-07-05 17:11:38.351 ttt[41418:5f03] 10000
2011-07-05 17:11:38.354 ttt[41418:1803] 10000dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
for (int i = 0; i<1000; i++) {
dispatch_async(aQueue, ^{
NSLog(@"%d",i);
});
}2011-07-05 17:15:37.525 ttt[41697:1803] 0
2011-07-05 17:15:37.526 ttt[41697:1803] 2
2011-07-05 17:15:37.527 ttt[41697:1803] 3
2011-07-05 17:15:37.527 ttt[41697:1803] 4
2011-07-05 17:15:37.527 ttt[41697:1803] 5
2011-07-05 17:15:37.527 ttt[41697:1803] 6
2011-07-05 17:15:37.526 ttt[41697:5f03] 1
2011-07-05 17:15:37.530 ttt[41697:5f03] 8
2011-07-05 17:15:37.530 ttt[41697:5f03] 9
2011-07-05 17:15:37.530 ttt[41697:5f03] 10
2011-07-05 17:15:37.530 ttt[41697:5f03] 11
2011-07-05 17:15:37.532 ttt[41697:6203] 13void myFinalizerFunction(){
NSLog(@"xxx");
}
- (void)viewDidLoad {
[superviewDidLoad];
dispatch_queue_t queue = dispatch_queue_create("com.example.MyQueue", NULL);
dispatch_set_context(queue, @"xxx");
dispatch_set_finalizer_f(queue, &myFinalizerFunction);
for (int i = 0; i<1000; i++) {
dispatch_async(queue, ^{
NSLog(@"%d",i);
});
}
dispatch_release(queue);7. dispatch_sync(queue,task)
会阻塞当前线程 直到queue完成了你给的task, 但queue要完成你给的task,因为queue是FIFO的,意味着要完成之前的任务,才有机会执行你刚才给的task, 相当于当前线程等待queue里面所有任务执行完毕, 所以这句话不能在当前queue的任务代码里面调用,会造成死锁dispatch_queue_t queue = dispatch_queue_create("com.example.MyQueue", NULL);
dispatch_set_context(queue, @"xxx");
dispatch_set_finalizer_f(queue, &myFinalizerFunction);
for (int i = 0; i<10; i++) {
dispatch_async(queue, ^{
NSLog(@"%d",i);
});
}
NSLog(@"waiting");
dispatch_sync(queue, ^{
NSLog(@"wait done");
});
dispatch_release(queue);2011-07-05 17:54:35.479 ttt[44203:207] waiting
2011-07-05 17:54:35.479 ttt[44203:1803] 0
2011-07-05 17:54:35.481 ttt[44203:1803] 1
2011-07-05 17:54:35.482 ttt[44203:1803] 2
2011-07-05 17:54:35.482 ttt[44203:1803] 3
2011-07-05 17:54:35.483 ttt[44203:1803] 4
2011-07-05 17:54:35.483 ttt[44203:1803] 5
2011-07-05 17:54:35.484 ttt[44203:1803] 6
2011-07-05 17:54:35.484 ttt[44203:1803] 7
2011-07-05 17:54:35.485 ttt[44203:1803] 8
2011-07-05 17:54:35.485 ttt[44203:1803] 9
2011-07-05 17:54:35.486 ttt[44203:207] wait done
2011-07-05 17:54:35.487 ttt[44203:1803] xxx8. Waiting on Groups of Queued Tasks //dispatch_sync只能对一个queue等待, group可以等好几个queue执行完
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
// Add a task to the group
dispatch_group_async(group, queue, ^{
NSLog(@"first task");
});
dispatch_queue_t otherqueue = dispatch_queue_create("com.example.MyQueue", NULL);
for (int i = 0; i<100; i++) {
dispatch_group_async(group, otherqueue, ^{
NSLog(@"otherqueue task");
});
}
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
NSLog(@"end waiting");
2011-07-05 19:32:31.919 ttt[50138:5f03] otherqueue task
2011-07-05 19:32:31.919 ttt[50138:1803] first task
2011-07-05 19:32:31.922 ttt[50138:5f03] otherqueue task
2011-07-05 19:32:31.923 ttt[50138:5f03] otherqueue task
...
2011-07-05 19:32:32.078 ttt[50138:5f03] otherqueue task
2011-07-05 19:32:32.079 ttt[50138:5f03] otherqueue task
2011-07-05 19:32:32.080 ttt[50138:207] end waitingvoid SaveArrayAsync(CFArrayRef array) {
CFRetain(array);
dispatch_queue_t queue = dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
SaveToDisk(array);
CFRelease(array);
}
}
// inside your controller's tableView:cellForRowAtIndexPath:
cell = [[MyCell alloc] initWithBlock:^(MyCell* cell) {
[self adjustCell:cell];
}];
self
. But that self
is the view controller. So you have a view controller retained by one of its descendants. This creates a retained object loop that ultimately means this controller will never get released.__block
variables are actually not retained when the lock is copied. So you can do this// inside your controller's tableView:cellForRowAtIndexPath:
__block me = self;
cell = [[MyCell alloc] initWithBlock:^(MyCell* cell) {
[me adjustCell:cell];
}];
Since the self doesn’t get retained this time, we avoid the retain cycle. While it strikes me as iffy, it does work, and it’s the only way I’ve found to deal with this situation. You can’t simply avoid copying the block if you want to call it later, because the scope will be gone by the time the function that declared the block exits. That will lead to nothing but sadness.
发表评论
-
手动编程 NSLayoutConstraint
2017-08-09 23:51 635项目中有个admob view是后期加上的 没写在st ... -
iOS autolayout 下 键盘 遮挡 处理 keyboard handler move up
2017-08-09 23:41 763在ios 中 由于键盘出现后会遮挡屏幕下面区域, ... -
iOS 开发一年多总结下
2012-12-19 16:47 1785收获很多 1. 一个人包办从构思, 设计, 实现, ... -
libxml2 xpath 解析含有 prefix namespace 的 xml
2012-11-06 13:28 3450有一段xml 里面有 prefix URL namespac ... -
iOS 6 设备转向问题
2012-09-21 00:33 3594iOS 6 对设备转向时 View Rotations 的处理 ... -
ios5 支持 json 转化
2011-10-29 21:58 7403------ 解析json成dic对象 - ... -
Quartz 2D (ProgrammingWithQuartz) note
2011-10-09 20:03 2882http://www.evernote.com/shard/s ... -
ios 添加字体
2011-10-09 19:09 2050<!--?xml version="1.0&q ... -
MBProgressHUD 画不出来解决办法
2011-09-02 14:34 2200- (void)tableView:(UITabl ... -
ios googlemap 添加map overlay
2011-09-02 14:32 2843画线 CLLocationCo ... -
ios 系统相关
2011-09-02 14:28 1485判断当前设备 是否ipad #define ... -
ios 5 才执行的代码
2011-09-02 13:03 1259设置navigationBar背景在ios5有了官方支持 ... -
UIPopoverController 相关问题
2011-09-02 12:57 1538Popover + tableView 的宽度问题 ... -
Settings 设置问题
2011-09-02 12:05 9961. setting面板的设置只有当用户去了设置面 ... -
ios 国际化相关
2011-09-02 12:04 1754添加国际化 1. 代码中使用字符串的地方 使 ... -
UITableView 相关问题
2011-09-02 11:53 1892动态调整cell高度 - (UITable ... -
提取ipa png 图标
2011-08-10 11:19 6230Apple 对 iPhone 应用程序中的 png 图片进 ... -
个人申请 idp
2011-07-19 15:15 1724要准备的东西 一张双币信用卡(扣$99用) 一张可以接受外汇 ... -
本人第一款ios app上架了
2011-07-19 12:26 1490第一次开发ios app, 希望大家多多支持, 多多下载, ... -
MKPointAnnotation 导致的内存泄露
2011-07-11 21:59 1515程序是在一个map 上添加annotation, 添加的时候有 ...
相关推荐
Grand Central Dispatch 如何终止子线程
全称:Grand Central Dispatch(GCD) 定义:GCD是苹果公司开发的一套多线程编程的API,用于简化多线程编程的复杂性。 功能:GCD提供了一个易于使用的、基于任务的并发模型,可以将任务提交给系统,系统会自动管理...
通过这篇文章,你将学会dispatch_async、dispatch_after、dispatch_...github.com/pro648/tips/wiki/Grand-Central-Dispatch的使用 源码下载:github.com/pro648/BasicDemos-iOS/tree/master/GrandCentralDispatch
Apple的Grand Central Dispatch(GCD)的防锈包装。 GCD是任务并行性的一种实现,它允许将任务提交到用于Apple的Grand Central Dispatch(GCD)的Rust包装器的队列。 GCD是任务并行性的一种实现,它允许将任务提交到...
To Steven Paul Jobs: From Mac OS’s very first incarnation, to the present one, wherein the legacy of NeXTSTEP still lives, his relationship with Apple is forever entrenched in OS X (and iOS)....
用于Grand Central Dispatch的Objective-C包装器,具有用于每个单个调度调用的方法以及一些有用的补充。 欢迎以请求请求,功能请求或问题的形式进行贡献! 特别建议使用改进的调度源,调度IO和调度数据API。 加法 ...
#GCDispatch# 继承自:NSObject 声明于:GCDispatch.h ##概述## GCDispatch类为使用 Grand Central Dispatch 队列执行块提供了一个简单的接口。 要使用此类,只需运行最适合您的并发需求的方法。 ##任务## #...
异步的Objective-C中的语法糖,用于Grand Central Dispatch( )中的异步调度注意:这是Swift中的 。 异步糖看起来像这样: [[Async main: ^{ NSLog ( @" ===>>> This is run on the main queue " );}] background: ...
Grand Central Dispatch FreeBSD DevsummitRobert Watson rwatson@FreeBSD.org 18-Sep-2009mailto:rwatson@FreeBSD.orgmailto:rwatson@FreeBSD.orglibdispatch ·runtime library for Grand Central ...
大中华区用 Swift 编写的 Grand Central Dispatch 的包装器。例子GCD // submit your code for asynchronous execution on a global queue with high prioritygcd. async (. High ) { // your code}// or with main ...
大中央派遣Grand Central Dispatch(GCD或libdispatch)为多核硬件上的并发代码执行提供全面的支持。 libdispatch当前可在所有Darwin平台上使用。 该项目旨在使libdispatch的现代版本可在所有其他Swift平台上使用。 ...
异步OC 用Objective-C重写... 代替熟悉的GCD语法: dispatch_async (dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0 ), ^{ NSLog ( @" A: This is run on the background " ); dispatch_async ( dis
在这本书中你将掌握云服务、自动引用计数、storyboard,Interface Builder, 创建ad-hoc 并且在模拟器生成, provision profiles, graphics, animations, Grand Central Dispatch, Operations 和Operation Queues, ...
在这本书中你将掌握云服务、自动引用计数、storyboard,Interface Builder, 创建ad-hoc 并且在模拟器生成, provision profiles, graphics, animations, Grand Central Dispatch, Operations 和Operation Queues, ...
在这本书中你将掌握云服务、自动引用计数、storyboard,Interface Builder, 创建ad-hoc 并且在模拟器生成, provision profiles, graphics, animations, Grand Central Dispatch, Operations 和Operation Queues, ...
在这本书中你将掌握云服务、自动引用计数、storyboard,Interface Builder, 创建ad-hoc 并且在模拟器生成, provision profiles, graphics, animations, Grand Central Dispatch, Operations 和Operation Queues, ...
如操作对象(Operation objects)和Grand Central Dispatch(GCD),提供一个更加现代化和高效率的基础设施来实现多核并发,但是Mac OS X和iOS也提供一套接口来创建和管理线程。 本文档介绍了Mac OS X上面的可用线程...
虽然更新的技术 如操作对象(Operation objects)和 Grand Central Dispatch(GCD),提供一个更 加现代化和高效率的基础设施来实现多核并发,但是 Mac OS X 和 iOS 也提供一套接 口来创建和管理线程。 本文档介绍了 Mac ...
Grand Central Dispatch (GCD)是异步执行任务的技术之一。一般将应用程序中记述的线程管理用的代码在系统级中实现。由于线程管理是作为系统的一部分来实现的,因此可统一管理,也可执行任务,这样就比以前的线程更...
Pro Multithreading and Memory Management for iOS and OS X with ARC, Grand Central Dispatch, and Blocks