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)