作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 goroutines 感到困惑。
这是代码
func main() {
// runtime.GOMAXPROCS(1)
go spinner(100 * time.Millisecond)
const n = 45
fibN := fib(n) // slow
fmt.Printf("\rFibonacci(%d) = %d\n", n, fibN)
}
func spinner(delay time.Duration) {
for {
for _, r := range `-\|/` {
fmt.Printf("\r%c", r)
time.Sleep(delay)
}
}
}
func fib(x int) int {
if x < 2 {
return x
}
return fib(x-1) + fib(x-2)
}
GOMAXPROCS
设置为
1
时,我认为将只有一个线程来执行goroutine,而Fibonacci函数对动画goroutine毫无意义。但是此演示仍然有效。计算时显示动画。
最佳答案
其中:编译器在每个函数调用处插入潜在的切换点,因此对fib(...)
的每个递归调用都可以产生“旋转器” goroutine。
如果您尝试在没有任何函数调用的情况下实现fib,例如:
// note : this is a truly horrific way to compute the Fibonacci sequence,
// don't do this at home
// simulate the "compute Fibonacci recursively" algorithm,
// but without any function call
func fib(n int) int {
var res = 0
var stack []int
stack = append(stack, n)
for len(stack) > 0 {
// pop :
n = stack[len(stack)-1]
stack = stack[0 : len(stack)-1]
if n < 2 {
res += n
continue
}
// else : push 'n-1' and 'n-2' on the stack
stack = append(stack, n-1, n-2)
}
return res
}
关于go - goroutine日程安排如何与GOMAXPROCS一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60743341/
我正在尝试 Gitlab“CI/CD > Container Registry 标签过期策略”设置,到目前为止它没有删除任何内容。我们使用语义版本控制(带有“v”前缀),我的目标是自动删除旧的“补丁”
我正在创建一个基于 html 和 css 的日程表/日历,但我遇到了一些问题。 这是我目前拥有的:http://jsfiddle.net/Lpfg5/ 我想要的是能够展开任何“ block ”,例如“
我是一名优秀的程序员,十分优秀!