<p>Is there a performance hit from starting goroutines like this ? Is there a comparable Thread executor (in Java) i.e. goroutine executor ?</p>
<hr/>**评论:**<br/><br/>tylermumford: <pre><blockquote>
<p>Is there a performance hit from starting goroutines like this ?</p>
</blockquote>
<p>Just to be clear, this is the only way to start a goroutine. You probably know this, but your question could imply that there are more ways to start goroutines, which is false.</p></pre>hell_0n_wheel: <pre><p>The answer is always "it depends", especially when you aren't telling us exactly how you're using the goroutine.</p>
<p>Just your luck, there's a standard library that allows you to measure the performance yourself: <a href="https://golang.org/pkg/testing/" rel="nofollow">https://golang.org/pkg/testing/</a></p>
<p>Wrap your code in a test, and run the test using <code>testing.B</code>. Tweak the code, re-run, bam, there's your answer.</p></pre>victrolla: <pre><p>There is a negligible performance hit but it would be highly dependent on what's going on. If you're asking about memory penalties, this is a non issue on modern systems. Goroutines do not have the memory overhead of the full runtime. </p>
<p>If you're asking about CPU penalty, you need to test. It's not clear what your goroutines will be doing but on modern multi core machines, you likely won't notice anything but I encourage you to use the excellent profiler. Even if the goroutine blocks, you're not likely to realize it as long as there aren't severe resource contention. </p>
<p>When you will notice is when you do things in goroutines like tight select loops. </p>
<p>I'm not familiar with java, but if the name hints to me what that is, you may want to look at the package sync.WaitGroup. </p>
<p>Edit: side note, usually things slow down when you start using a mutex to coordinate things between goroutines. As a design principal you likely want to favor sending the data across a channel to avoid extraneous locks. </p></pre>titpetric: <pre><p>You can read <a href="https://rakyll.org/scheduler/" rel="nofollow">this article for more details on the Go scheduler</a>. The scheduler is basically similar to what a thread scheduler is in the OS kernel - it schedules work between goroutines. The performance hit is smaller than starting actual threads. I wouldn't know about a comparable thread executor, but the concept of 'coroutines' is well known in other programming languages as well. Perhaps this is a question for <a href="/r/java" rel="nofollow">/r/java</a> more than here.</p>
<p>Edit: <a href="https://stackoverflow.com/questions/2846664/implementing-coroutines-in-java" rel="nofollow">this stackoverflow post might provide some info into the state of goroutines in Java</a>.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传