Java 为什么 Kotlin 中没有并发关键字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35520583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why there are no concurrency keywords in Kotlin?
提问by vach
Why there are no keywords for synchronization/concurrency?
为什么没有同步/并发的关键字?
So far my research gives me one solution, you wrap some high level classes and use them to handle concurrency.
到目前为止,我的研究给了我一个解决方案,你包装一些高级类并使用它们来处理并发。
Given a project in pure Kotlin, what one shall do if there is a need for a small highly optimized component that handles concurrency etc?
给定一个纯 Kotlin 的项目,如果需要一个小的高度优化的组件来处理并发等,该怎么办?
My impression is that Kotlin is an assisting language for Java, to write 90% of the code in Kotlin but have some java code that is not possible to express with Kotlin.
我的印象是Kotlin是Java的辅助语言,90%的代码都是用Kotlin写的,但是有一些Java代码是Kotlin无法表达的。
Is this right? Is this how it was intended to be?
这是正确的吗?这是它的意图吗?
采纳答案by Jayson Minard
Kotlin 1.1 with Coroutines was releasedand it brings with it async..await
! Read more about it in Kotlin reference docs, Kotlinx Coroutines libraryand this great in depth Couroutines by Example
带有 Coroutines 的 Kotlin 1.1 发布并带来了它async..await
!在Kotlin 参考文档、Kotlinx Coroutines 库和这个非常深入的Coroutines by Example 中阅读更多关于它的信息
Outside of the Kotlin Coroutines, you have these options:
在 Kotlin 协程之外,您还有以下选项:
- the Kovenant libraryadds Promises to Kotlin
- the Quasar libraryprovides light-weight threads and continuations
@Synchronized
and@Volatile
annotations which map directly to the same keywords in Javasynchronized
blockswhich in Kotlin come from an inline functionsynchronized()
.- Kotlin has a
Kotlin.concurrent
package and extensionswith new functions and also extensions to JDK classes. - you can access anything in the
java.util.concurrent
packagesuch asConcurrentHashMap
,CountdownLatch
,CyclicBarrier
,Semaphore
, ... - you can access anything in the
java.util.concurrent.locks
packageand Kotlin has extensions for a few of theseincluding the coolwithLock()
extension functionand similarread
/write
extensionsforReentrantReadWriteLock
. - you can access anything in the
java.util.concurrent.atomic
packagesuch asAtomicReference
,AtomicLong
, ... - you can use
wait
andnotify
on objects
- 在Kovenant库增加了应许科特林
- 的类星体库提供轻量线程和延续
@Synchronized
和@Volatile
直接映射到 Java 中相同关键字的注释synchronized
Kotlin 中的块来自内联函数synchronized()
。- Kotlin 有一个包含新功能的
Kotlin.concurrent
包和扩展以及对 JDK 类的扩展。 - 您可以访问
java.util.concurrent
包中的任何内容,例如ConcurrentHashMap
,CountdownLatch
,CyclicBarrier
,Semaphore
, ... - 您可以在访问任何
java.util.concurrent.locks
包装和科特林有少数的这些扩展包括酷withLock()
扩展功能和类似的read
/write
扩展的ReentrantReadWriteLock
。 - 您可以访问
java.util.concurrent.atomic
包中的任何内容,例如AtomicReference
,AtomicLong
, ... - 您可以在对象上使用
wait
和notify
You have everything Java has and more. Your phrase "synchronization and locks"is satisfied by the list above, and then you have even more and without language changes. Any language features would only make it a bit prettier.
您拥有 Java 所拥有的一切以及更多。上面的列表满足了您的短语“同步和锁定”,然后您拥有更多并且没有语言更改。任何语言特性只会让它更漂亮一点。
So you can have 100% Kotlin code, using the small Kotlin runtime, the JVM runtime from the JDK, and any other JVM library you want to use. No need for Java code, just Java (as-in JVM) libraries.
因此,您可以拥有 100% 的 Kotlin 代码,使用小型 Kotlin 运行时、来自 JDK 的 JVM 运行时以及您想要使用的任何其他 JVM 库。不需要 Java 代码,只需要 Java (as-in JVM) 库。
A quick sample of some features:
一些功能的快速示例:
class SomethingSyncd {
@Synchronized fun syncFoo() {
}
val myLock = Any()
fun foo() {
synchronized(myLock) {
// ... code
}
}
@Volatile var thing = mapOf(...)
}
回答by vach
I'll answer my own question since actual answer to my question was somewhere deep in kotlin discussions.
我会回答我自己的问题,因为我的问题的实际答案是在 kotlin 讨论的深处。
What confused me at the time comming from java was that concurrency keywords were not language keywords they were annotations? to me it seemed strange that important concepts like synchronization were handled trough annotation, but now it makes perfect sense. Kotlin is going in the direction of being platform agnostic language, its not going to only work on jvm but pretty much anything. So synchronized and volatile were very specific to jvm, they might not be needed in javascript for example.
当时来自 java 的让我感到困惑的是,并发关键字不是它们是注释的语言关键字?对我来说,像同步这样的重要概念是通过注释处理的,这似乎很奇怪,但现在它完全有道理了。Kotlin 正朝着成为平台无关语言的方向发展,它不仅适用于 jvm,而且几乎适用于任何东西。因此,synchronized 和 volatile 非常特定于 jvm,例如,在 javascript 中可能不需要它们。
In a nutshell kotlin has everything java has (except package visibility) and much more, a huge difference that no other language has is coroutines. But there is nothing you can write in java that you cant do in kotlin... (as far as i am aware)
简而言之,kotlin 拥有 java 所拥有的一切(包可见性除外),而且还有更多其他语言所没有的巨大差异是协程。但是没有什么可以在 Java 中编写而在 kotlin 中无法完成的......(据我所知)