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() // 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() 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 := ; count < ; count++ {
for char := 'a'; char < 'a'+; 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 := ; count < ; count++ {
for char := 'A'; char < 'A'+; char++ {
fmt.Printf("%c ", char)
}
fmt.Println()
}
}() // Wait for the goroutines to finish.
fmt.Println("Waiting To Finish")
wg.Wait() fmt.Println("\nTerminating 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
上一篇:PAT Basic 1001


下一篇:3.Git基础-查看当前文件状态、跟踪新文件、暂存文件、忽略文件、提交更新、移除文件、移动文件