In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to understand goroutine, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
(1)。 Suppose there is a project that requires three engineering teams to complete, their construction events are independent of each other, who builds quickly and who finishes that part first, and the others do not need to wait for other engineering teams.
Package main
Import (
"fmt"
"time"
)
Func main () {
Fmt.Println ("start construction")
Go workTwo ()
Go workThree ()
Go workOne ()
Fmt.Println ("waiting for the total construction of the project to be completed")
Time.Sleep (time.Second * 3)
}
/ / workOne Engineering team 1 built part of it.
Func workOne () {
WorkTime: = time.Millisecond * 3000 / / time to complete the project
Time.Sleep (workTime)
Fmt.Println ("Construction completed: workOne")
}
/ / workTwo Engineering team 2 built part
Func workTwo () {
WorkTime: = time.Millisecond * 2500 / / time to complete the project
Time.Sleep (workTime)
Fmt.Println ("Construction completed: workTwo")
}
/ / workThree Engineering team 3 built part of it.
Func workThree () {
WorkTime: = time.Millisecond * 2000 / / time to complete the project
Time.Sleep (workTime)
Fmt.Println ("Construction completed: workThree")
}
(2)。 Add acceptance function to the project and use go chan to realize it.
Package main
Import (
"fmt"
"time"
)
Const (
WorkOneDone = "oneDone"
WorkTwoDone = "twoDone"
WorkThreeDone = "threeDone"
)
Func main () {
Fmt.Println ("start construction")
Ch: = make (chan string, 1)
Go func () {
WorkTwo (ch)
} ()
Go func () {
WorkThree (ch)
} ()
Go func () {
WorkOne (ch)
} ()
Go func () {
Test (ch)
} ()
/ / close chan
Defer close (ch)
/ / waiting for the completion of the general project
Time.Sleep (time.Second * 5)
}
Func test (ch chan string) {
For {
Select {
Case v: =
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.