In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "what is concurrency in Go language". In daily operation, I believe that many people have doubts about what concurrency is in GE language. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what concurrency is in Go language". Next, please follow the editor to study!
Concurrency of Go language
The Go language directly supports built-in support for concurrency. When a function is created as goroutine, Go treats it as a separate unit of work. This unit is scheduled to be executed on an available logical processor.
The scheduler of the Go language runtime is a complex software that is on top of the operating system. The operating system thread binds to the logical processor of the language runtime and runs goroutine on the logical processor.
The concurrent synchronization logic of the Go language comes from a paradigm called CSP. CSP is a messaging model that delivers messages by passing data between goroutine rather than by locking the data for synchronous access. This type of data is called channel.
Concurrency and parallelism
In the operating system, an application can be thought of as a process, and each process contains at least one thread. The initial thread of each process is called the main thread.
The operating system will schedule threads to run on the physical processor (CPU), while the Go language will schedule goroutine to run on the logical processor. Above version 1.5, the runtime of the Go language allocates one logical processor to each available physical processor by default. Prior to 1.5, only one logical processor was assigned to the entire application by default.
As shown in the following figure, the goroutine is scheduled to run on the logical processor at run time, and the logical processor is bound to a unique operating system thread.
When goroutine executes a blocked system call (that is, a non-pure CPU task), the scheduler separates the thread from the processor and creates a new thread to run the services provided on the processor.
Language runs limit the creation of up to 10000 threads per program by default.
Pay attention to concurrent ≠ parallelism! Parallelism requires at least 2 logical processors.
Goroutine
Display uppercase and lowercase letters concurrently
Package main
Import (
"fmt"
"runtime"
"sync"
Func main () {
/ / assign a logical processor to the scheduler for use
Runtime.GOMAXPROCS (1)
/ / wg is used to wait for the program to finish.
Var wg sync.WaitGroup
/ / add 2 to the counter, which means to wait for two goroutine
Wg.Add (2)
Fmt.Println ("Start!")
/ / declare an anonymous function and create a goroutime
Go func () {
/ / notify main that the work of the function has been completed
Defer wg.Done ()
21: / / display the alphabet 3 times
22: for count:=0; count
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.