<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ### Gosched() 使當前Go協程放棄處理器,讓其他協程運行。不會掛起當前協程,未來恢復執行。 Go語言的協程是搶占式調度,當遇到長時間執行或者系統調用時,會主動把當前goroutine的CPU(P)轉讓出去,讓其他的goroutine能夠被調用并執行,一般出現以下幾種情況,goroutine就會發生調度: * [ ] ayscall * [ ] C函數調用 * [ ] 主動調用runtime.Goschen * [ ] 某個goroutine的調用超過100ms,并且調用了非內聯的函數 > **runtime.Gosched()** > 由于main函數搶占了CPU的控制權,導致第一個for循環沒有運行完。如果想要第一個for循環執行,在第二個for循環中使用runtime.Gosched()來阻止獲得控制權 ``` package main import ( "fmt" // "runtime" ) func main() { go func() { for i := 0; i < 3; i++ { fmt.Println("go") } }() for i := 0; i < 2; i++ { // runtime.Gosched() fmt.Println("main") } } 結果: main main go ``` ### Goexit() 終止調用的協程,其他的不受影響 > **runtime.Goexit()** ``` package main import ( "fmt" "runtime" "time" ) func main() { go func() { defer fmt.Println("defer關鍵詞 會讓表達式最后執行") fmt.Println("Hello World") }() go func() { defer fmt.Println("defer關鍵詞 會讓表達式最后執行") fmt.Println("你好,世界!") runtime.Goexit() // 效果和return一樣 fmt.Println("這里不會執行") }() time.Sleep(time.Second * 5) } 結果: 你好,世界! Hello World defer關鍵詞 會讓表達式最后執行 defer關鍵詞 會讓表達式最后執行 ``` ### GOMAXPROCS() 設置程序運行中使用的CPU數,可設置同時執行最大CPU數。 > runtime.GOMAXPROCS() ``` package main import ( "fmt" "runtime" "time" ) func main() { n := runtime.GOMAXPROCS(1) fmt.Println("先前最大核心數為", n) // 運行時間戳 毫秒 last := time.Now() // 耗時任務 for i := 0; i < 10000000; i++ { a := 1 a += 1 } now := time.Now() // 計算程序運行的時間 fmt.Println(now.Sub(last)) } 結果: 先前最大核心數為 12 2.1563ms ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看