The concurrent chapter of Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Process and thread A. A process is a process in which a program executes in an operating system, and the system performs a separate unit of resource allocation and scheduling. B. A thread is an executing entity of a process that is the basic unit of CPU dispatch and dispatch, which is a smaller unit that can run independently than a process. C. A process can create and revoke multiple threads, and can execute concurrently between multiple threads in the same process.

Concurrent and parallel Concurrency: Multithreaded threads run parallel on one core CPU: Multi-threaded routines run on multiple cores of CPUs
Example.. A mother gives a bowl to multiple children feeding, which is concurrent
A mother gives each child a bowl, which is parallel.

concurrency parallel

Cheng and line: Independent stack space, shared heap space, scheduling by the user's own control, essentially a bit similar to the user-level process, these user-level thread scheduling is also implemented by themselves. Threads: One thread can run multiple threads, and the co-threads are lightweight.

Example

Package Mainimport ("FMT"    " Time") func Test () {varIint     for{fmt. Println (i) time. Sleep (time. Second) I++}}func Main () {go test ()//a co-Process Execution test ()     for{fmt. Println ("i:runnging in main") time. Sleep (time. Second)}}

-

--

Sets the number of CPU cores run by the Golang.

More than 1.8 versions, run multiple cores by default

Package Mainimport (    "fmt"    "runtime"  func Main () {    num:= runtime. NUMCPU ()    runtime. Gomaxprocs (num)    FMT. PRINTLN (num)}

Communication between different goroutine (A, global variables and lock synchronization B. Channle)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.