TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Go's Concurrency Examples in Java 19

151 点作者 emccue大约 3 年前

13 条评论

ibraheemdev大约 3 年前
&gt; There is no way for your operating system to know exactly how much stack space a thread will need so it allocates an amount on the order of around a megabyte. You only have around a bakers dozen gigabytes of RAM, so you can only make give or take 10,000 threads.<p>That&#x27;s not true at all. The megabyte(s) of stack space is virtual, spawning a thread only requires a few kilobytes of memory initially, and is a lot cheaper than many realize.
评论 #31256515 未加载
评论 #31256342 未加载
评论 #31256276 未加载
评论 #31256281 未加载
评论 #31257281 未加载
pron大约 3 年前
People sometimes respond with some puzzlement to InterruptedException, unsure what they should do. All it means is that some other thread has requested that this thread stop what it&#x27;s doing. So do what you think your code should do in that situation. E.g. in the case of `say`, the most reasonable thing is to simply return.<p>You shouldn&#x27;t normally call `Thread.currentThread().interrupt()` inside a `catch (InterruptedException e)` unless, perhaps, you don&#x27;t throw anything. That would prevent any blocking operation inside cleanup code from working. As a general rule, a thread&#x27;s interrupt status is set <i>or</i> InterruptedException (or some other exception) is thrown, but not both. That is how JDK methods also do it. If they throw — they clear the interrupt status.
评论 #31259536 未加载
评论 #31259293 未加载
marginalia_nu大约 3 年前
While there are use cases where lightweight threads are a benefit, I think a web crawler is pretty much the ideal case for async&#x2F;await, given you are almost exclusively waiting. Although I must admit I don&#x27;t really understand this example, why is the fetching being parallellized, like you typically want to add sleeps, even with a single thread you&#x27;ve got yourself a gatling gun that can DoS a website if you aren&#x27;t careful.<p>There&#x27;s also some optimizations that could be made. Instead of having<p><pre><code> synchronized (seen) { if (!seen.contains(u)) { seen.add(u); &#x2F;&#x2F; ... } } </code></pre> you can get rid of the synchronization with<p><pre><code> &#x2F;&#x2F; seen = ConcurrentHashMap.newKeySet() if (seen.add(u)) { &#x2F;&#x2F; ... }</code></pre>
评论 #31255922 未加载
评论 #31256488 未加载
ziihrs大约 3 年前
In Go, the select branch to take is non-deterministically chosen from the set of enabled branches. For the examples I don&#x27;t think it makes a difference, but in general you cannot assume that a select attempts to choose branches from top to bottom.
评论 #31258179 未加载
funcDropShadow大约 3 年前
Loom&#x27;s virtual thread are much more similar to GHC&#x27;s threads than to go routines. And GHC Haskell is also able to spawn millions of threads, trivially.
评论 #31259321 未加载
matttproud大约 3 年前
&gt; In Go there is less noise, but also there no way to interrupt Go&#x27;s time.Sleep.<p>You can do so in one of two ways using the context API (<a href="https:&#x2F;&#x2F;go.dev&#x2F;blog&#x2F;context" rel="nofollow">https:&#x2F;&#x2F;go.dev&#x2F;blog&#x2F;context</a>) while keeping the code synchronous and idiomatic:<p><pre><code> package main import ( &quot;context&quot; &quot;fmt&quot; &quot;time&quot; ) func say(ctx context.Context, s string) { for i := 0; i &lt; 5; i++ { select { case &lt;-time.After(100 * time.Millisecond): fmt.Println(s) case &lt;-ctx.Done(): return &#x2F;&#x2F; Operation canceled or timed out. } } } func main() { ctx := context.Background() &#x2F;&#x2F; Use context cancellation semantics. go say(ctx, &quot;world&quot;) say(ctx, &quot;hello&quot;) } </code></pre> If the code is reasonably performance sensitive AND there&#x27;s a high degree of likelihood the context is canceled before the operation completes, you can use a time.Timer similar to what follows:<p><pre><code> for i := 0; i &lt; 5; i++ { func(i int) { &#x2F;&#x2F; Wrap in closure expressly to support defer statement. t := time.NewTimer(100 * time.Millisecond) defer t.Stop() select { case &lt;-t.C: fmt.Println(s) case &lt;-ctx.Done(): return &#x2F;&#x2F; Operation canceled or timed out. } }(i) }</code></pre>
xyproto大约 3 年前
Judging by the examples, the Go code is easier to read, write and maintain.
评论 #31258727 未加载
评论 #31258171 未加载
评论 #31258300 未加载
评论 #31258170 未加载
评论 #31258383 未加载
deepsun大约 3 年前
Go solution to TreeWalk problem is wrong, or, rather, not real-world, because it has a memory leak. You need to make sure all goroutines finish, and not stuck on reading&#x2F;writing to a channel.<p>In Kotlin coroutines you can .cancel() a coroutine, but Go doesn&#x27;t allow to cancel goroutines.
rad_gruchalski大约 3 年前
time.Sleep(100 * time.Millisecond) could also be: &lt;-time.After(100 * time.Millisecond) so that an imaginary timeout can be easily handled with a select.
评论 #31258931 未加载
评论 #31256494 未加载
orthogonal-wren大约 3 年前
Good read! I just saw that in one examples you create and start a thread by invoking #startVirtualThread, and then you call start on the returned thread again.
评论 #31259381 未加载
nyanpasu64大约 3 年前
Interesting article history! Does your site have a RSS feed to keep up with updates in a feed reader? I couldn&#x27;t find one.
sidcool大约 3 年前
How is this different than Kotlin coroutines? They seem to already support this feature.
评论 #31256926 未加载
评论 #31257003 未加载
freakynit大约 3 年前
Does GO even makes sense after Java&#x27;s project loom and graalvm?
评论 #31256775 未加载
评论 #31256865 未加载
评论 #31260773 未加载
评论 #31256355 未加载
评论 #31256945 未加载
评论 #31259092 未加载
评论 #31256572 未加载
评论 #31256929 未加载