In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The editor will share with you an example analysis of the underlying principles of concurrent goroutine in the Go language. I hope you will gain something after reading this article. Let's discuss it together.
I. basic concept ① parallelism and parallelism
1. Concept
Concurrency: one object performs multiple tasks in the same period of time, making full use of time
Parallel: multiple objects perform multiple tasks at the same time
two。 Graphic illustration
Similar to supermarket counter checkout, parallel is multiple counters with multiple queues, multicore cpu handles multiple threads opened by go language in the computer, and concurrency is a counter checkout with multiple queues. In the computer, single-core cpu handles multiple tasks and grabs time slices.
② expounds the advantages and disadvantages of concurrency of go and java from user mode thread and kernel mode thread.
1. Difference between user mode thread and kernel mode thread
User mode: only restricted access to memory, and access to peripheral devices is not allowed, the occupation of CPU resources can be taken away by other programs.
Kernel mode: CPU can access all data in memory, including peripherals, such as hard disk network cards, and cpu can switch itself from one program to another.
Concurrency differences between 2.java and go:
Java:
Java does not specify which thread to use, but switches between different threads, which will consume considerable resources.
Go is a user-mode thread that consumes less resources. The stack of a thread defaults to 1m and needs to run on JVM.
Go:
Go language is implemented concurrently through goroutine, which belongs to user-mode threads. Thousands of goroutine can be created as needed, and the memory occupied by each goroutine will be dynamically generated as needed. The typical size is 2kB, which can be magnified to 1GB on demand. In go language, you can easily create about 100, 000 goroutine at a time, and do not depend on the running environment.
Why is ② high concurrency a Go language strength?
1. Historical background
The Go language came into being relatively late, and there was multicore cpu before it came into being, so the designer's idea is to use this new language on multicore cpu to support a larger number of concurrency.
two。 One's own reason
The underlying implementation of multi-concurrency in Go language uses cooperative programs, which has fewer resources and faster execution speed, and the resources occupied will be expanded or reduced according to the amount of tasks.
Principle Analysis of High concurrency low-level GMP Model implemented by ③ Go language
1. G:
G is the abbreviation of Goroutine. In this case, it is the control structure of Goroutine and the abstraction of Goroutine. It includes the function instructions and parameters executed; the task objects saved by G; the registers (SP, IP) needed for thread context switching, on-site protection and on-site recovery. The runtime.g structure is used in the Go language.
2. M:
Indicates that operating system threads, also known as kernel threads, are scheduled and managed by the operating system, and the scheduler can create up to 10000 threads, which are represented by runtime.m structures in the Go language. (mapping relationship between user thread and kernel thread)
3. P:
Schedule each goroutine to coordinate the operation of logical processors, but it does not represent the number of real CPU. What really determines the degree of concurrency is P, which usually reads the corresponding value of GOMAXPROCS during initialization. If there is no display setting, it will read the default value. After Go1.5, GOMAXPROCS is defaulted to set the number of available cores, while before it defaults to 1, which is represented by runtime.p structure in Go language.
4. Specify the number of CPU threads
With runtime.GOMAXPROCS (), you can specify the number of Ps. If not specified, the entire cpu will run by default.
Second, code to learn Go language and ①. Start a simple thread
Start a thread to use the go+ function. In the following case, you need to recognize the problems that may arise when opening a multithreaded function closure.
1. Start a thread using an anonymous function
/ / print 1-1000 for I: = 0; I < 1000; iTunes + {go func () {fmt.Println (I)} ()} / / functional closures / * print results 995 995 996 996 999 1000 1000
two。 The principle of the problem:
The anonymous function closes the variables in the current environment when it operates. Because it takes a certain amount of time to start the thread, I changes when starting the thread, so many values will be the same when printing.
two。 Dynamically shuts down threads
1. Why do you need to shut down threads dynamically?
If there is no dynamic thread closure in the GE language, it is possible that the main thread will end without the end of the child thread, so there will be a hidden danger to the program security, so the main thread can not be terminated directly, but should be used as a backing. It can't end until all threads are finished.
two。 Use waitGroup
There are three common methods for waitGroup:
WaitGroup.Add (): use the wait counter to record 1 times / / to pass in the number of threads created
WaitGroup.Done (): wait counter minus 1 (in the function of the opened thread)
WaitGroup.Wait (): blocking wait wait counter value is zero (placed in the main thread)
Defer is the code that executes when the function body finishes execution (which can be understood as deferred execution).
The code is as follows:
Package mainimport ("fmt"math/rand"sync"time") / / define a waitGroup structure variable var wg sync.WaitGroupfunc f (I int) {/ / until the function finishes executing The counter in waitGroup will be subtracted by one defer wg.Done () rand.Seed (time.Now (). UnixNano ()) time.Sleep (time.Duration (rand.Intn (3)) * time.Second) fmt.Println (I)} func main () {for I: = 0 I < 100 Wg.Add (1) go f (I)} / blocking wait until the waitGroup counter is 0 wg.Wait () fmt.Println ("hello")} after reading this article, I believe you have a certain understanding of "sample Analysis of the underlying principles of concurrent goroutine in Go language". If you want to know more about it. Welcome to follow the industry information channel, thank you for reading!
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.