File tree Expand file tree Collapse file tree 1 file changed +6
-6
lines changed Expand file tree Collapse file tree 1 file changed +6
-6
lines changed Original file line number Diff line number Diff line change 1
1
# 2.7 并发
2
2
3
- 有人把Go比作21世纪的C语言,第一是因为Go语言设计简单,第二,21世纪最重要的就是并行程序设计,而GO从语言层面就支持了并行 。
3
+ 有人把Go比作21世纪的C语言,第一是因为Go语言设计简单,第二,21世纪最重要的就是并行程序设计,而Go从语言层面就支持了并行 。
4
4
5
5
## goroutine
6
6
7
- goroutine是Go并行设计的核心。goroutine说到底其实就是线程,但是他比线程更小 ,十几个goroutine可能体现在底层就是五六个线程,Go语言内部帮你实现了这些goroutine之间的内存共享。执行goroutine只需极少的栈内存(大概是4~ 5KB),当然会根据相应的数据伸缩。也正因为如此,可同时运行成千上万个并发任务。goroutine比thread更易用、更高效、更轻便。
7
+ goroutine是Go并行设计的核心。goroutine说到底其实就是线程,但是它比线程更小 ,十几个goroutine可能体现在底层就是五六个线程,Go语言内部帮你实现了这些goroutine之间的内存共享。执行goroutine只需极少的栈内存(大概是4~ 5KB),当然会根据相应的数据伸缩。也正因为如此,可同时运行成千上万个并发任务。goroutine比thread更易用、更高效、更轻便。
8
8
9
9
goroutine是通过Go的runtime管理的一个线程管理器。goroutine通过` go ` 关键字实现了,其实就是一个普通的函数。
10
10
@@ -159,7 +159,7 @@ channel通过操作符`<-`来接收和发送数据
159
159
case c <- x:
160
160
x, y = y, x + y
161
161
case <-quit:
162
- fmt.Println("quit")
162
+ fmt.Println("quit")
163
163
return
164
164
}
165
165
}
@@ -187,7 +187,7 @@ channel通过操作符`<-`来接收和发送数据
187
187
}
188
188
189
189
## 超时
190
- 有时候会出现goroutine阻塞的情况,那么我们如何避免整个的程序进入阻塞的情况呢 ?我们可以利用select来设置超时,通过如下的方式实现:
190
+ 有时候会出现goroutine阻塞的情况,那么我们如何避免整个程序进入阻塞的情况呢 ?我们可以利用select来设置超时,通过如下的方式实现:
191
191
192
192
func main() {
193
193
c := make(chan int)
@@ -225,11 +225,11 @@ runtime包中有几个处理goroutine的函数:
225
225
226
226
- NumGoroutine
227
227
228
- 返回正在执⾏行和排队的任务总数
228
+ 返回正在执行和排队的任务总数
229
229
230
230
- GOMAXPROCS
231
231
232
- 用来设置可以运行的CPU核数
232
+ 用来设置可以并行计算的CPU核数的最大值,并返回之前的值。
233
233
234
234
235
235
You can’t perform that action at this time.
0 commit comments