这种并发相关的类怎么称呼呢?是否有标准实施?
因为我找不到它的标准实现,所以我创建了这个小类,但我认为一些简单的东西应该已经存在于某个地方:
class ReturnValue {
private var value = false
private val latch = new java.util.concurrent.CountDownLatch(1)
def setValue(aValue: Boolean) {
value = aValue
latch.countDown()
}
def getValue() = {
latch.await
value
}
}
它的目的是在两个线程之间交换值(在我的例子中是主线程+ EDT) 。从使用 getValue() 方法的代码来看,它看起来几乎像一个 Future,但我发现 Future 实现期望调用者提供要执行的代码,这在我的情况下不起作用。
所以我的问题是:
- 这样的东西有名字吗?
- java 或 scala 中有标准实现吗?
- 我的实现是否正确?
Since I couldn't find a standard implementation of it I created this little class, but I think something simple as this should exist somewhere already:
class ReturnValue {
private var value = false
private val latch = new java.util.concurrent.CountDownLatch(1)
def setValue(aValue: Boolean) {
value = aValue
latch.countDown()
}
def getValue() = {
latch.await
value
}
}
The purpose of it is to exchange a value between two threads (main thread + EDT in my case). From the code using the getValue() method it looks almost like a Future, but the Future implementations I found expect the caller to provide the code to be executed, which doesn't work in my case.
So my questions are:
- Is there a name for such a thingy?
- Is there a standard implementation for it in java or scala?
- Is my implementation even correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Scala 标准库提供了这种同步机制作为 scala.concurrent.SyncVar[T] 类。
此 Scala REPL 会话演示了它的工作原理:
我导入
ops._
来轻松生成另一个Thread
。我正在生成另一个线程。
get
会阻塞,直到syncInt
中有一个值。上面的内容已经被我们之前创建的线程打印出来了。
Scala standard library provides this kind of synchronization mechanism as
scala.concurrent.SyncVar[T]
class.This Scala REPL session demonstrates how it works:
I am importing
ops._
to spawn anotherThread
easily.I am spawning another thread.
get
blocks until there is a value in thesyncInt
.The above has been printed by the thread we created before.
看起来有点类似于同步队列 使用一个元素,消费者必须 等待生产者提供一个值。
Looks somewhat similar to synchronous queue with one element, where consumer has to wait for producer to offer a value.
这看起来有点像
我同意你的观点,除了“可执行”部分之外,它看起来像是一个未来。
That looks a bit like an Exchanger to me, except it's more one-sided... have you looked at that? Basically you wouldn't need to worry about what you provided from the "waiting" side, or what you received from the "providing" part.
I agree with you that it looks like a future aside from the "executable" part.
我创建了一个非常相似的东西,称为 BlockingReference,我需要消费者(一个或多个)能够读取最新的值或块,直到有一个可用为止。本质上,它就像一个单元素队列,因为生产者线程可以随时发布新值然后继续。当唯一需要发布的信息是某种状态更新时,它是相关的。我用它来标记多线程内容分发缓存中的进度(其中一个线程下载内容,并且有多个消费者重新广播下载的字节)。与您的主要区别在于您的设备是一次性的。
此实现明显优于
SynchronousQueue
和Exchanger
,因为它们都会阻塞生产者线程,直到发生切换。它在性能上优于 Scala SyncVar ,因为生产者线程是在没有任何阻塞的情况下实现的,并且在功能上优于 Scala SyncVar ,因为它可以支持多个消费者。它已进行了大量性能优化。Atlassian Concurrency 库已获得 Apache2 许可,并且位于我们的公共 Maven 存储库中。
I created a very similar thing called a BlockingReference where I needed consumers (one or many) to be able to read the latest value or block until one became available. In essence it is like a single element queue as the producer thread can at any time post a new value and then carry on. It is relevant where the only information that needs to be posted is some kind of status update. I use it mark progress in a multi-threaded content-distribution cache (where one thread downloads content and there are multiple consumers rebroadcasting the downloaded bytes). The main difference to yours is that yours is single use.
This implementation is significantly superior to
SynchronousQueue
andExchanger
as both of those block the producer thread until a handoff occurs. It is superior to the ScalaSyncVar
in performance as the producer-thread is implemented without any blocking and in features as it can support multiple consumers. It has been heavily performance optimised.The Atlassian Concurrency lib is Apache2 licensed and is in our public Maven repo.
这与流编程有些关系。
This is somewhat related to flow programming.