这种并发相关的类怎么称呼呢?是否有标准实施?

发布于 2024-10-17 09:44:12 字数 578 浏览 1 评论 0原文

因为我找不到它的标准实现,所以我创建了这个小类,但我认为一些简单的东西应该已经存在于某个地方:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

想念有你 2024-10-24 09:44:12

Scala 标准库提供了这种同步机制作为 scala.concurrent.SyncVar[T] 类。

此 Scala REPL 会话演示了它的工作原理:

scala> import concurrent._           
import concurrent._

scala> import ops._                  
import ops._

我导入 ops._ 来轻松生成另一个 Thread

scala> val syncInt = new SyncVar[Int]
syncInt: scala.concurrent.SyncVar[Int] = scala.concurrent.SyncVar@17823918

scala> spawn { println("got %d" format syncInt.get) }

我正在生成另一个线程。 get 会阻塞,直到 syncInt 中有一个值。

scala> syncInt.isSet
res1: Boolean = false

scala> syncInt.set(103)

scala> got 103

上面的内容已经被我们之前创建的线程打印出来了。

scala> syncInt.isSet
res3: Boolean = true

scala> syncInt.get
res4: Int = 103

Scala standard library provides this kind of synchronization mechanism as scala.concurrent.SyncVar[T] class.

This Scala REPL session demonstrates how it works:

scala> import concurrent._           
import concurrent._

scala> import ops._                  
import ops._

I am importing ops._ to spawn another Thread easily.

scala> val syncInt = new SyncVar[Int]
syncInt: scala.concurrent.SyncVar[Int] = scala.concurrent.SyncVar@17823918

scala> spawn { println("got %d" format syncInt.get) }

I am spawning another thread. get blocks until there is a value in the syncInt.

scala> syncInt.isSet
res1: Boolean = false

scala> syncInt.set(103)

scala> got 103

The above has been printed by the thread we created before.

scala> syncInt.isSet
res3: Boolean = true

scala> syncInt.get
res4: Int = 103
尽揽少女心 2024-10-24 09:44:12

看起来有点类似于同步队列 使用一个元素,消费者必须 等待生产者提供一个值。

Looks somewhat similar to synchronous queue with one element, where consumer has to wait for producer to offer a value.

锦爱 2024-10-24 09:44:12

这看起来有点像

我同意你的观点,除了“可执行”部分之外,它看起来像是一个未来。

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.

奈何桥上唱咆哮 2024-10-24 09:44:12

我创建了一个非常相似的东西,称为 BlockingReference,我需要消费者(一个或多个)能够读取最新的值或块,直到有一个可用为止。本质上,它就像一个单元素队列,因为生产者线程可以随时发布新值然后继续。当唯一需要发布的信息是某种状态更新时,它是相关的。我用它来标记多线程内容分发缓存中的进度(其中一个线程下载内容,并且有多个消费者重新广播下载的字节)。与您的主要区别在于您的设备是一次性的。

此实现明显优于 SynchronousQueueExchanger,因为它们都会阻塞生产者线程,直到发生切换。它在性能上优于 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 and Exchanger as both of those block the producer thread until a handoff occurs. It is superior to the Scala SyncVar 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.

你爱我像她 2024-10-24 09:44:12

这与流编程有些关系。

This is somewhat related to flow programming.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文