indulgein / ybtaskscheduler Goto Github PK
View Code? Open in Web Editor NEWiOS 任务调度器,为 CPU 和内存减负(用于性能优化)
License: MIT License
iOS 任务调度器,为 CPU 和内存减负(用于性能优化)
License: MIT License
仔细看了代码,认为YBTaskScheduler的执行顺序逻辑并不可用。
双线队列固然能保证每个block任务在queue中是有序的(FIFO、LIFO、优先级),但最后执行的时候是在多个串行队列异步执行的。 然而多个异步线程之间的执行顺序其实是不能保证的,单纯的任务保存有序没有特别的价值。
static void addRunLoopObserver() { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ taskSchedulers = [NSHashTable weakObjectsHashTable]; CFRunLoopObserverRef observer = CFRunLoopObserverCreate(CFAllocatorGetDefault(), kCFRunLoopBeforeWaiting | kCFRunLoopExit, true, 0xFFFFFF, runLoopObserverCallBack, NULL); CFRunLoopAddObserver(CFRunLoopGetMain(), observer, kCFRunLoopCommonModes); CFRelease(observer); }); }
利用runloop,将我们收敛的任务执行在runloop运行期间。但是注意到,永远是在main runloop 触发这些任务的执行的。假如,我在子线程中添加了任务,但是主线程卡住了。意味着主线程的runloop 不能够及时的触发状态回调而触发任务执行。而受到牵连的在子线程添加的任务不能够及时执行。这种情况子线程的任务执行情况反而依赖于主线程的卡顿情况.
主线程的卡顿,反而影响到了子线程的执行效率
如果主线程极端卡顿的情况下。多线程执行的优势就不存在了~
- (void)ybts_addTask:(YBTaskBlock)task priority:(YBTaskPriority)priority {
if (!task) return;
pthread_mutex_lock(&_lock);
_deque.push_front(task);
if (self.ybts_maxNumberOfTasks > 0) {
while (_deque.size() > self.ybts_maxNumberOfTasks) {
_deque.pop_front();
}
}
pthread_mutex_unlock(&_lock);
}
YBTSQueue
和YBTSStack
这里没有看到根据优先级插入到相应位置的实现呢
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.