• go 并发 demo


    两个进程执行两个goroutine

    // 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(2)
    
        // 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 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 X Y Z 
    A B C D 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 
    E F G H I J K L M N O P Q R S T U V W X Y Z 

    一个进程执行两个goroutine

    // 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 
  • 相关阅读:
    Codeforces Round #113 (Div. 2) Tetrahedron(滚动DP)
    Codeforces Round #300 Quasi Binary(DP)
    Codeforces Round #119 (Div. 2) Cut Ribbon(DP)
    Codeforces Round #260 (Div. 1) Boredom(DP)
    Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Cards Sorting(树状数组)
    Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Office Keys(思维)
    图灵杯 E 简单的RMQ(UVA 11235)(RMQ)
    qwb与学姐 (带秩并查集)
    计蒜客 UCloud 的安全秘钥(困难)(哈希)
    第八届山东省ACM大学生程序设计竞赛个人总结
  • 原文地址:https://www.cnblogs.com/sea-stream/p/10327961.html
Copyright © 2020-2023  润新知