使用线程安全的 MSWeakTimer ,它不会对目标进行retain操作,避免循环引用

简介:

MSWeakTimer

简易翻译:该timer没有runloop概念,线程安全,没有循环引用现象。

https://github.com/mindsnacks/MSWeakTimer

Description

Thread-safe NSTimer drop-in alternative that doesn't retain the target and supports being used with GCDqueues.

这是个线程安全的 NSTimer,他不会对目标进行retain操作,而且,他还支持GCDqueues。

Motivation

The first motivation for this class was to have a type of timer that objects could own and retain, without this creating a retain cycle ( like NSTimer causes, since it retains its target ). This way you can just release the timer in the -dealloc method of the object class that owns the timer.

写这个类的动机是,我想使用这样的NSTimer,他不会对object进行retain操作,而且,更不会出现循环引用(系统的NSTimer就会出现这个问题)。然后呢,你可以在-dealloc方法中来释放这个timer。

The other problem when using NSTimer is this note on the documentation:

另一个使用NSTimer会出现的问题在以下文档中提出了:

Special Considerations

You must send this message from the thread on which the timer was installed. If you send this message from another thread, the input source associated with the timer may not be removed from its run loop, which could prevent the thread from exiting properly.

需要特别注意的地方

你必须在当前的线程中给你创建的NSTimer发送信息,如果你是从另外一个线程中发过来的信息,那么,这个timer可能就不会从他当前的run loop中移除,那个会很出现问题的。

More often than not, an object needs to create a timer and invalidate it when a certain event occurs. However, doing this when that object works with a private GCD queue gets tricky. This timer object is thread safe and doesn't have the notion of run loop, so it can be used with GCD queues and installed / invalidated from any thread or queue.

不管怎样,一个对象创建出一个定时器,当触发了几次事件后,就销毁这个定时器。然而,你如果在GCD队列中来销毁这个定时器,效果非常微妙(有可能出现内存泄露)。而大哥我写的这个定时器是线程安全的,他不存在run loop的概念,所以,他可以在任何的GCDqueue中进行初始化以及移除掉。

Related Stackoverflow question.

Implementation

The implementation of MSWeakTimer was reviewed and validated by a libdispatch (GCD) engineer at the WWDC 2013 Core OS Lab.

How to Use

Create an MSWeakTimer object with this class method:

+ (MSWeakTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)timeInterval
                                         target:(id)target
                                       selector:(SEL)selector
                                       userInfo:(id)userInfo
                                        repeats:(BOOL)repeats
                                  dispatchQueue:(dispatch_queue_t)dispatchQueue;
目录
相关文章
|
5天前
|
Java 数据库 Android开发
【专栏】Kotlin在Android开发中的多线程优化,包括线程池、协程的使用,任务分解、避免阻塞操作以及资源管理
【4月更文挑战第27天】本文探讨了Kotlin在Android开发中的多线程优化,包括线程池、协程的使用,任务分解、避免阻塞操作以及资源管理。通过案例分析展示了网络请求、图像处理和数据库操作的优化实践。同时,文章指出并发编程的挑战,如性能评估、调试及兼容性问题,并强调了多线程优化对提升应用性能的重要性。开发者应持续学习和探索新的优化策略,以适应移动应用市场的竞争需求。
|
2天前
|
Python
|
5天前
|
Java 数据库
【Java多线程】对线程池的理解并模拟实现线程池
【Java多线程】对线程池的理解并模拟实现线程池
17 1
|
1天前
|
NoSQL Redis 缓存
【后端面经】【缓存】36|Redis 单线程:为什么 Redis 用单线程而 Memcached 用多线程?
【5月更文挑战第17天】Redis常被称为单线程,但实际上其在处理命令时采用单线程,但在6.0后IO变为多线程。持久化和数据同步等任务由额外线程处理,因此严格来说Redis是多线程的。面试时需理解Redis的IO模型,如epoll和Reactor模式,以及其内存操作带来的高性能。Redis使用epoll进行高效文件描述符管理,实现高性能的网络IO。在讨论Redis与Memcached的线程模型差异时,应强调Redis的单线程模型如何通过内存操作和高效IO实现高性能。
28 7
【后端面经】【缓存】36|Redis 单线程:为什么 Redis 用单线程而 Memcached 用多线程?
|
3天前
|
监控 Java 测试技术
在多线程开发中,线程死循环可能导致系统资源耗尽,影响应用性能和稳定性
【5月更文挑战第16天】在多线程开发中,线程死循环可能导致系统资源耗尽,影响应用性能和稳定性。为解决这一问题,建议通过日志记录、线程监控工具和堆栈跟踪来定位死循环;处理时,及时终止线程、清理资源并添加错误处理机制;编码阶段要避免无限循环,正确使用同步互斥,进行代码审查和测试,以降低风险。
18 3
|
5天前
|
设计模式 消息中间件 安全
【Java多线程】关于多线程的一些案例 —— 单例模式中的饿汉模式和懒汉模式以及阻塞队列
【Java多线程】关于多线程的一些案例 —— 单例模式中的饿汉模式和懒汉模式以及阻塞队列
12 0
|
5天前
|
Java
【Java多线程】分析线程加锁导致的死锁问题以及解决方案
【Java多线程】分析线程加锁导致的死锁问题以及解决方案
26 1
|
5天前
|
存储 缓存 安全
【Java多线程】线程安全问题与解决方案
【Java多线程】线程安全问题与解决方案
22 1
|
5天前
|
Java 调度
【Java多线程】线程中几个常见的属性以及状态
【Java多线程】线程中几个常见的属性以及状态
13 0