kotlin 原子计数器
“Kotlin for Lunch” collects Kotlin exercises, usage patterns, or “the Kotlin way” to solve development issues. This article should be short enough to have fun with Kotlin during a short lunch break.
“午餐的科特琳”收集Kotlin练习,用法模式或“Kotlin方式”来解决开发问题。 本文应该足够简短,以便在短暂的午餐时间与Kotlin一起玩乐。
We are here today: Kotlin > Coroutines > Concurrency
我们今天在这里:Kotlin>协程>并发
Kotlins Mutex is a nice idea when you need to lock a code block from beeing executed at the same time, but the code examples show them almost always together with a variable:
当您需要锁定代码块以防止同时执行蜂鸣操作时,Kotlins Mutex是个不错的主意,但是代码示例几乎总是将它们与变量一起显示:
演示地址
Mutex example (shortened) from the official Kotlin docs. 互斥体示例(简化),来自 Kotlin官方 文档。So why not have a builtin variable that can only be modified when the Mutex is locked? If you look at the Kotlin docs, you find AtomicInt, AtomicLong, and a few others — but not for all platforms (“native” only), and nowhere a generic Atomic<T>. Its time to change that.
那么,为什么不具有只能在Mutex被锁定时才能修改的内置变量呢? 如果您查看Kotlin文档,则会发现AtomicInt , AtomicLong和其他一些文件-但不是针对所有平台(仅适用于“本机”),而没有通用的Atomic<T> 。 是时候改变它了。
So lets introduce Atomic<T>, an object that allows you to modify the value of type T, when no one else can. Even better, lets inherit from Mutex, so every Atomic<T> becomes also a Mutex and you get the Mutex functions for free.
因此,让我们介绍Atomic<T> ,这是一个您可以在没有其他人可以修改的情况下修改T类型的值的对象。 更好的是,让我们从Mutex继承,因此每个Atomic<T>也会成为Mutex,您可以免费获得Mutex函数。
Mutex() is an extension function and provides a Mutex object that implements that interface. By delegating the implementation to this object, we save all the function forwarding and get a very slim class definition. All functions and properties from Mutex are available without any boilerplate.
Mutex()是扩展函数,并提供实现该接口的Mutex对象。 通过将实现委派给该对象,我们保存了所有函数转发,并获得了非常苗条的类定义。 Mutex所有功能和特性均无需任何样板即可使用。
演示地址
Lets try it out with some code:
让我们尝试一些代码:
演示地址
Works nice. But wait a second, having to call a variable inside a lambda that was called from that very same variable doesn’t look good, or as others say, it smells.
效果不错。 但是请稍等片刻,不得不在从同一变量调用的lambda内调用一个变量,看起来并不好,或者就像其他人所说的那样,它闻起来很香。
It would be much more cool (and maybe less error prone and more intuitive) if we would have this referring to our Atomic<T> or T object itself.
如果我们将this引用为Atomic<T>或T对象本身,那将更酷(并且可能更不容易出错且更直观)。
We need a function that allows us the set the new value and a function that let us read or modify our T as this. I came up with these two:
我们需要一个可以让我们设定的新的价值,并且让我们读取或修改我们的功能T的this 。 我想出了这两个:
演示地址
withLock allows us to use a lambda that can use value as this. That enables you to use functions from T without any boilerplate. You can either manipulate this directly or just read (and return) whatever you need in a consistent blocked way.
withLock允许我们使用lambda可以使用value作为this 。 这样一来,您无需任何样板即可使用T功能。 您可以直接操作它,也可以以一致的阻塞方式读取(并返回)所需的任何内容。
setWithLock allows us to put in a lambda that receives the current value of our Atomic<T> and saves the returned value as new state. That makes nicer code as your T object stays immutable.
setWithLock允许我们放入一个lambda来接收Atomic<T>的当前值,并将返回值保存为新状态。 由于您的T对象保持不变,因此代码更好。
An interesting code gimmick of Kotlin is the way to call the original Mutex.withLock function. Issue here - Mutex.withLock is not a member, but an extension function (see also the extra import line). So you can’t reach it with super.withLock, but you need to cast your object to a Mutex, to reach out for Mutex.withLock.
Kotlin一个有趣的代码Mutex.withLock是调用原始Mutex.withLock函数的方法。 这里的问题Mutex.withLock不是成员,而是扩展函数(另请参见额外的import行)。 因此,您无法使用super.withLock到达它,但是您需要将对象Mutex.withLock转换为Mutex ,以获取Mutex.withLock 。
Everything put together with an example program to run:
一切都与示例程序一起运行:
演示地址
Hope you enjoyed. Ideas and remarks are welcome.
希望你喜欢。 欢迎提出想法和意见。
翻译自: https://medium.com/@freiit/kotlin-for-lunch-atomic-t-261351048fad
kotlin 原子计数器
相关资源:四史答题软件安装包exe