zoukankan      html  css  js  c++  java
  • go 单进程并发

    demo1

    // This sample program demonstrates how to create goroutines and
    // how the scheduler behaves.
    package main
    
    import (
        "fmt"
        "runtime"
        "sync"
    )
    
    // main is the entry point for all Go programs.
    func main() {
        // Allocate 1 logical processor for the scheduler to use.
        runtime.GOMAXPROCS(1)
    
        // wg is used to wait for the program to finish.
        // Add a count of two, one for each goroutine.
        var wg sync.WaitGroup
        wg.Add(2)
    
        fmt.Println("Start Goroutines")
    
        // Declare an anonymous function and create a goroutine.
        go func() {
            // Schedule the call to Done to tell main we are done.
            defer wg.Done()
    
            // Display the alphabet three times
            for count := 0; count < 3; count++ {
                for char := 'a'; char < 'a'+26; char++ {
                    fmt.Printf("%c ", char)
                }
                fmt.Println()
            }
        }()
    
        // Declare an anonymous function and create a goroutine.
        go func() {
            // Schedule the call to Done to tell main we are done.
            defer wg.Done()
    
            // Display the alphabet three times
            for count := 0; count < 3; count++ {
                for char := 'A'; char < 'A'+26; char++ {
                    fmt.Printf("%c ", char)
                }
                fmt.Println()
            }
        }()
    
        // Wait for the goroutines to finish.
        fmt.Println("Waiting To Finish")
        wg.Wait()
    
        fmt.Println("
    Terminating Program")
    }

    输出

    Start Goroutines
    Waiting To Finish
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
    a b c d e f g h i j k l m n o p q r s t u v w x y z 
    a b c d e f g h i j k l m n o p q r s t u v w x y z 
    a b c d e f g h i j k l m n o p q r s t u v w x y z 
  • 相关阅读:
    职业生涯起步不要去顶级公司
    discuz uc密码修改
    习惯决定成功与否
    世上没有理想的工作
    中山市慧海人力资源服务有限公司
    Codeforces Round #365 (Div. 2) B 前缀和
    Codeforces Round #365 (Div. 2) A 水
    tyvj 1067 dp 两次LIS(nlogn)
    Codeforces Round #303 (Div. 2) D 贪心
    Codeforces Round #303 (Div. 2) C dp 贪心
  • 原文地址:https://www.cnblogs.com/sea-stream/p/10357689.html
Copyright © 2011-2022 走看看