<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 一、概念 * runtime.Gosched():出讓CPU時間片,等待重新安排任務 * runtime.Goexit():立刻結束當前協程的執行 * runtime.GOMAXPROCS():設置并行計算的CPU最大核數 ## 二、runtime.Gosched()實例 #### 沒有runtime.Gosched() ~~~ package main import ( "fmt" ) func main() { // 子協程 go func(s string) { for i:=0;i<3;i++ { fmt.Println(s) } }("hello") // 主協程 for i:=1;i<3;i++ { fmt.Println("aaa") } } ~~~ 執行結果: hello hello hello aaa aaa #### 有runtime.Gosched() 注:通過上面多次執行我們發現,很多情況下都先打印出主協程中的內容,如果我們想更多的機會先打印子協程中的內容那么我們可以在主協程中加runtime.Gosched() ~~~ package main import ( "fmt" "runtime" ) func main() { // 子協程 go func(s string) { for i:=0;i<3;i++ { fmt.Println(s) } }("hello") // 主協程 for i:=1;i<3;i++ { runtime.Gosched() fmt.Println("aaa") } } ~~~ 打印結果: hello hello hello aaa aaa ## 三、runtime.Goexit() #### 沒有runtime.Goexit() ~~~ package main import ( "fmt" ) func main() { // 子協程 go func() { defer fmt.Println("A.defer") func(){ defer fmt.Println("B.defer") fmt.Println("B") }() defer fmt.Println("A") }() // 主協程 for i:=1;i<3;i++ { fmt.Println("aaa") } } ~~~ 打印結果: B B.defer A A.defer aaa aaa #### 有runtime.Goexit() ~~~ package main import ( "fmt" "runtime" ) func main() { // 子協程 go func() { defer fmt.Println("A.defer") func(){ defer fmt.Println("B.defer") runtime.Goexit() fmt.Println("B") }() defer fmt.Println("A") }() // 主協程 for i:=1;i<3;i++ { fmt.Println("aaa") } } ~~~ 打印結果: aaa aaa 注: runtime.Goexit()不影響defer的執行 ## 三、runtime.GOMAXPROCS() ~~~ package main import ( "fmt" "runtime" ) func main() { // 使用4個核去執行程序 n := runtime.GOMAXPROCS(4) fmt.Printf("n=%d\n",n) // 假設我們讓一個核去執行一個程序 for{ go fmt.Print(0) fmt.Print(1) } } ~~~
                  <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>

                              哎呀哎呀视频在线观看