问题标签 [goroutine]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
go - 你能检测出给定数量的 goroutine 将创建多少个线程吗?
我知道 goroutine 被多路复用到多个操作系统线程上,所以如果一个应该阻塞,比如在等待 I/O 时,其他的会继续运行。但是有没有办法提前知道如果我要创建 n 个 goroutine 会产生多少线程?
例如,如果我们调用下面的函数,我们是否知道将为 n 个 goroutine 创建多少(或最大数量)系统线程:
go - Shared memory vs. Go channel communication
One of Go's slogans is Do not communicate by sharing memory; instead, share memory by communicating.
I am wondering whether Go allows two different Go-compiled binaries running on the same machine to communicate with one another (i.e. client-server), and how fast that would be in comparison to boost::interprocess in C++? All the examples I've seen so far only illustrate communication between same-program routines.
A simple Go example (with separate client and sever code) would be much appreciated!
go - 如何在不阻塞的情况下确定 goroutine 是否已完成?
到目前为止,我看到的所有示例都涉及阻塞以获取结果(通过<-chan
操作员)。
我目前的方法涉及将指针传递给结构:
goroutine 在完成时写入的内容。finished
然后在方便的时候进行检查是一件简单的事情。你有更好的选择吗?
我真正的目标是 Qt 风格的信号槽系统。我有一种预感,解决方案看起来几乎是微不足道的(chan
s 有很多未开发的潜力),但我对语言还不够熟悉,无法弄清楚。
parallel-processing - goroutine 的最小工作量
有谁知道大约需要多少最小工作量才能使 goroutine 受益(假设有空闲核心可以卸载工作)?
concurrency - 是否可以从 Go 中的多个 goroutine 之一接收结果?
我最近才了解了 Google 的编程语言 Go。我对它提供的并发支持很感兴趣,并着手了解更多有关它的信息。然而,我去看看 Go 是如何实现一个特定的并发特性的,到目前为止我还没有看到任何证据表明这个特性是存在的。
这是一个假设的情况:假设我们正在编写一个函数来确定特定输入的 Foo 值。对于任何给定的输入,Foo 值可以在域 A 或域 B 中找到(不在两者中)。这些领域的搜索技术大相径庭,但它们都有一个共同的特点,即成功的搜索往往会很快返回,而不成功的搜索必须遍历整个数据集才能详尽无遗,因此需要很长时间。
现在,在其他使用并发的语言(例如Cilk)中,可以对函数 Foosearch 进行编程,以便它产生一个 Asearch 函数和一个 Bsearch 函数。这些函数将同时运行,并且每当它们中的任何一个提出答案时,该答案都会报告给调用函数 Foosearch,该函数将终止它产生的任何未返回的函数。
然而,使用 Go 的 goroutine,您似乎只能将两个例程与一个通道连接 - 因此您无法设置 Asearch 或 Bsearch 可以发送到的通道,具体取决于哪个首先找到答案,并让 Foosearch 从它。看起来您也无法在不阻塞的情况下从通道中读取 - 所以您不能让 Foosearch 启动 Asearch 和 Bsearch 并从两者设置通道,然后在循环中运行以查看其中一个或另一个是否产生了答案。
我对 Go 并发限制的理解是否正确?还有另一种方法可以达到给定的结果吗?
java - Go 和 Java 使用用户空间线程这一事实是否意味着您不能真正利用多核?
我们最近在我的操作系统课上讨论了很多线程,我想到了一个问题。
由于 Go(和 Java)使用用户空间线程而不是内核线程,这是否意味着您不能有效地利用多个内核,因为操作系统只将 CPU 时间分配给进程而不是线程本身?
java - How to asynchronously call a method in Java
I've been looking at Go's goroutines lately and thought it would be nice to have something similar in Java. As far as I've searched the common way to parallelize a method call is to do something like:
Thats not very elegant. Is there a better way of doing this? I needed such a solution in a project so I decided to implement my own wrapper class around a async method call.
I published my wrapper class in J-Go. But I don't know if it is a good solution. The usage is simple:
or less verbose:
Internally I'm using a class that implements Runnable and do some Reflection work to get the correct method object and invoking it.
I want some opinion about my tiny library and on the subject of making async method calls like this in Java. Is it safe? Is there already a simplier way?
go - 强制 goroutine 进入同一个线程
有没有办法确保 goroutine 只在特定的 OS 线程中运行?例如,当 GUI 操作必须在 GUI 线程中运行,但可能有多个 goroutines 运行 GUI 代码时。
GOMAXPROCS(1)
在技术上完成这项工作,但这违背了多线程的目的。
LockOSThread()
也可以,但这也阻止了任何其他 goroutine 在该线程中运行。
有没有办法做到这一点,或者需要同一个线程的所有东西都必须在同一个 goroutine 中运行?
multithreading - go map 结构是线程安全的吗?
Go map 类型的线程安全吗?我有一个程序,它有许多 goroutine 读取和写入映射类型。如果我需要实施保护机制,最好的方法是什么?