forked from gobyexample-cn/gobyexample-cn.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstateful-goroutines.html
298 lines (241 loc) · 17.3 KB
/
stateful-goroutines.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example 中文版: 状态协程</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'mutexes';
}
if (e.key == "ArrowRight") {
window.location.href = 'sorting';
}
}
</script>
<body>
<div class="example" id="stateful-goroutines">
<h2><a href="./">Go by Example 中文版</a>: 状态协程</h2>
<table>
<tr>
<td class="docs">
<p>在前面的例子中,我们用 <a href="mutexes">互斥锁</a> 进行了明确的锁定,
来让共享的 state 跨多个 Go 协程同步访问。
另一个选择是,使用内建协程和通道的同步特性来达到同样的效果。
Go 共享内存的思想是,通过通信使每个数据仅被单个协程所拥有,即通过通信实现共享内存。
基于通道的方法与该思想完全一致!</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="https://play.studygolang.com/p/0TeN3xKw_2l"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kn">import</span> <span class="p">(</span>
<span class="s">"fmt"</span>
<span class="s">"math/rand"</span>
<span class="s">"sync/atomic"</span>
<span class="s">"time"</span>
<span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>在这个例子中,state 将被一个单独的协程拥有。
这能保证数据在并行读取时不会混乱。
为了对 state 进行读取或者写入,
其它的协程将发送一条数据到目前拥有数据的协程中,
然后等待接收对应的回复。
结构体 <code>readOp</code> 和 <code>writeOp</code> 封装了这些请求,并提供了响应协程的方法。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">type</span> <span class="nx">readOp</span> <span class="kd">struct</span> <span class="p">{</span>
<span class="nx">key</span> <span class="kt">int</span>
<span class="nx">resp</span> <span class="kd">chan</span> <span class="kt">int</span>
<span class="p">}</span>
<span class="kd">type</span> <span class="nx">writeOp</span> <span class="kd">struct</span> <span class="p">{</span>
<span class="nx">key</span> <span class="kt">int</span>
<span class="nx">val</span> <span class="kt">int</span>
<span class="nx">resp</span> <span class="kd">chan</span> <span class="kt">bool</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>和前面的例子一样,我们会计算操作执行的次数。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">var</span> <span class="nx">readOps</span> <span class="kt">uint64</span>
<span class="kd">var</span> <span class="nx">writeOps</span> <span class="kt">uint64</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>其他协程将通过 <code>reads</code> 和 <code>writes</code> 通道来发布 <code>读</code> 和 <code>写</code> 请求。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">reads</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="kd">chan</span> <span class="nx">readOp</span><span class="p">)</span>
<span class="nx">writes</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="kd">chan</span> <span class="nx">writeOp</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>这就是拥有 <code>state</code> 的那个协程,
和前面例子中的 map 一样,不过这里的 state 是被这个状态协程私有的。
这个协程不断地在 <code>reads</code> 和 <code>writes</code> 通道上进行选择,并在请求到达时做出响应。
首先,执行请求的操作;然后,执行响应,在响应通道 <code>resp</code> 上发送一个值,表明请求成功(<code>reads</code> 的值则为 state 对应的值)。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="k">go</span> <span class="kd">func</span><span class="p">()</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">state</span> <span class="p">=</span> <span class="nb">make</span><span class="p">(</span><span class="kd">map</span><span class="p">[</span><span class="kt">int</span><span class="p">]</span><span class="kt">int</span><span class="p">)</span>
<span class="k">for</span> <span class="p">{</span>
<span class="k">select</span> <span class="p">{</span>
<span class="k">case</span> <span class="nx">read</span> <span class="o">:=</span> <span class="o"><-</span><span class="nx">reads</span><span class="p">:</span>
<span class="nx">read</span><span class="p">.</span><span class="nx">resp</span> <span class="o"><-</span> <span class="nx">state</span><span class="p">[</span><span class="nx">read</span><span class="p">.</span><span class="nx">key</span><span class="p">]</span>
<span class="k">case</span> <span class="nx">write</span> <span class="o">:=</span> <span class="o"><-</span><span class="nx">writes</span><span class="p">:</span>
<span class="nx">state</span><span class="p">[</span><span class="nx">write</span><span class="p">.</span><span class="nx">key</span><span class="p">]</span> <span class="p">=</span> <span class="nx">write</span><span class="p">.</span><span class="nx">val</span>
<span class="nx">write</span><span class="p">.</span><span class="nx">resp</span> <span class="o"><-</span> <span class="kc">true</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}()</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>启动 100 个协程通过 <code>reads</code> 通道向拥有 state 的协程发起读取请求。
每个读取请求需要构造一个 <code>readOp</code>,发送它到 <code>reads</code> 通道中,
并通过给定的 <code>resp</code> 通道接收结果。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="k">for</span> <span class="nx">r</span> <span class="o">:=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">r</span> <span class="p"><</span> <span class="mi">100</span><span class="p">;</span> <span class="nx">r</span><span class="o">++</span> <span class="p">{</span>
<span class="k">go</span> <span class="kd">func</span><span class="p">()</span> <span class="p">{</span>
<span class="k">for</span> <span class="p">{</span>
<span class="nx">read</span> <span class="o">:=</span> <span class="nx">readOp</span><span class="p">{</span>
<span class="nx">key</span><span class="p">:</span> <span class="nx">rand</span><span class="p">.</span><span class="nf">Intn</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span>
<span class="nx">resp</span><span class="p">:</span> <span class="nb">make</span><span class="p">(</span><span class="kd">chan</span> <span class="kt">int</span><span class="p">)}</span>
<span class="nx">reads</span> <span class="o"><-</span> <span class="nx">read</span>
<span class="o"><-</span><span class="nx">read</span><span class="p">.</span><span class="nx">resp</span>
<span class="nx">atomic</span><span class="p">.</span><span class="nf">AddUint64</span><span class="p">(</span><span class="o">&</span><span class="nx">readOps</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="nx">time</span><span class="p">.</span><span class="nf">Sleep</span><span class="p">(</span><span class="nx">time</span><span class="p">.</span><span class="nx">Millisecond</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}()</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>用相同的方法启动 10 个写操作。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="k">for</span> <span class="nx">w</span> <span class="o">:=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">w</span> <span class="p"><</span> <span class="mi">10</span><span class="p">;</span> <span class="nx">w</span><span class="o">++</span> <span class="p">{</span>
<span class="k">go</span> <span class="kd">func</span><span class="p">()</span> <span class="p">{</span>
<span class="k">for</span> <span class="p">{</span>
<span class="nx">write</span> <span class="o">:=</span> <span class="nx">writeOp</span><span class="p">{</span>
<span class="nx">key</span><span class="p">:</span> <span class="nx">rand</span><span class="p">.</span><span class="nf">Intn</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span>
<span class="nx">val</span><span class="p">:</span> <span class="nx">rand</span><span class="p">.</span><span class="nf">Intn</span><span class="p">(</span><span class="mi">100</span><span class="p">),</span>
<span class="nx">resp</span><span class="p">:</span> <span class="nb">make</span><span class="p">(</span><span class="kd">chan</span> <span class="kt">bool</span><span class="p">)}</span>
<span class="nx">writes</span> <span class="o"><-</span> <span class="nx">write</span>
<span class="o"><-</span><span class="nx">write</span><span class="p">.</span><span class="nx">resp</span>
<span class="nx">atomic</span><span class="p">.</span><span class="nf">AddUint64</span><span class="p">(</span><span class="o">&</span><span class="nx">writeOps</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="nx">time</span><span class="p">.</span><span class="nf">Sleep</span><span class="p">(</span><span class="nx">time</span><span class="p">.</span><span class="nx">Millisecond</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}()</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>让协程们跑 1s。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">time</span><span class="p">.</span><span class="nf">Sleep</span><span class="p">(</span><span class="nx">time</span><span class="p">.</span><span class="nx">Second</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>最后,获取并报告 <code>ops</code> 值。</p>
</td>
<td class="code">
<pre class="chroma">
<span class="nx">readOpsFinal</span> <span class="o">:=</span> <span class="nx">atomic</span><span class="p">.</span><span class="nf">LoadUint64</span><span class="p">(</span><span class="o">&</span><span class="nx">readOps</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"readOps:"</span><span class="p">,</span> <span class="nx">readOpsFinal</span><span class="p">)</span>
<span class="nx">writeOpsFinal</span> <span class="o">:=</span> <span class="nx">atomic</span><span class="p">.</span><span class="nf">LoadUint64</span><span class="p">(</span><span class="o">&</span><span class="nx">writeOps</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"writeOps:"</span><span class="p">,</span> <span class="nx">writeOpsFinal</span><span class="p">)</span>
<span class="p">}</span>
</pre>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
<p>运行这个程序显示这个基于协程的状态管理的例子
达到了每秒大约 80,000 次操作。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="gp">$</span> go run stateful-goroutines.go
<span class="go">readOps: 71708
</span><span class="go">writeOps: 7177</span></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>通过这个例子我们可以看到,基于协程的方法比基于互斥锁的方法要复杂得多。
但是,在某些情况下它可能很有用,
例如,当你涉及其他通道,或者管理多个同类互斥锁时,会很容易出错。
您应该使用最自然的方法,尤其是在理解程序正确性方面。</p>
</td>
<td class="code empty">
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="sorting.html">排序</a>
</p>
<p class="footer">
<a href="https://twitter.com/mmcgrana">@mmcgrana</a> 和<a href="https://eli.thegreenplace.net">Eli Bendersky</a>编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a> </p>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"math/rand\"\u000A \"sync/atomic\"\u000A \"time\"\u000A)\u000A');codeLines.push('type readOp struct {\u000A key int\u000A resp chan int\u000A}\u000Atype writeOp struct {\u000A key int\u000A val int\u000A resp chan bool\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var readOps uint64\u000A var writeOps uint64\u000A');codeLines.push(' reads :\u003D make(chan readOp)\u000A writes :\u003D make(chan writeOp)\u000A');codeLines.push(' go func() {\u000A var state \u003D make(map[int]int)\u000A for {\u000A select {\u000A case read :\u003D \u003C-reads:\u000A read.resp \u003C- state[read.key]\u000A case write :\u003D \u003C-writes:\u000A state[write.key] \u003D write.val\u000A write.resp \u003C- true\u000A }\u000A }\u000A }()\u000A');codeLines.push(' for r :\u003D 0; r \u003C 100; r++ {\u000A go func() {\u000A for {\u000A read :\u003D readOp{\u000A key: rand.Intn(5),\u000A resp: make(chan int)}\u000A reads \u003C- read\u000A \u003C-read.resp\u000A atomic.AddUint64(\u0026readOps, 1)\u000A time.Sleep(time.Millisecond)\u000A }\u000A }()\u000A }\u000A');codeLines.push(' for w :\u003D 0; w \u003C 10; w++ {\u000A go func() {\u000A for {\u000A write :\u003D writeOp{\u000A key: rand.Intn(5),\u000A val: rand.Intn(100),\u000A resp: make(chan bool)}\u000A writes \u003C- write\u000A \u003C-write.resp\u000A atomic.AddUint64(\u0026writeOps, 1)\u000A time.Sleep(time.Millisecond)\u000A }\u000A }()\u000A }\u000A');codeLines.push(' time.Sleep(time.Second)\u000A');codeLines.push(' readOpsFinal :\u003D atomic.LoadUint64(\u0026readOps)\u000A fmt.Println(\"readOps:\", readOpsFinal)\u000A writeOpsFinal :\u003D atomic.LoadUint64(\u0026writeOps)\u000A fmt.Println(\"writeOps:\", writeOpsFinal)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>