This is a creation in Article, where the information may have evolved or changed.
concurrency (concurrency) and parallel (parallelism) are different. In a single CPU core, the thread through the time slice or give up control to achieve task switching, to "simultaneously" transport ⾏ multiple Tasks ⺫, which is called concurrency. But in fact, only one task is executed at any moment, and other tasks are queued by some sort of algorithm. Multi-core CPUs allow the "multiple threads" in the same ⼀ process to be transported in real time, without queuing between them (the queue can still occur because the number of threads may exceed the number of CPU cores, there are other processes, and so on.) This is an ideal condition), which is parallel. In addition to multicore, parallel computing may also be a deployment run on more than one machine.
Package Mainimport ("FMT""Runtime") func Test (C ChanBOOLNint) {x: =0 forI: =0; I <1000000000; i++ {x + = I}PRINTLN (n, x)ifn = =9{C <-true}}func Main () {runtime. Gomaxprocs (1)//Set the number of cores in the CPU to achieve high concurrencyc: = Make (chanBOOL) forI: =0; I <Ten; i++ {Go Test (c, i)}<-cfmt. Println ("main OK")}
The test results are as follows:
Using the results of one core
[' Go run Sendmail.go ' | done:24.6124077s] Main OK0 4999999995000000001 4999999995000000002 4999999995000000003 4999999995000000004 4999999995000000005 4999999995000000006 4999999995000000007 4999999995000000008) 4999999995000000009 499999999500000000
Setting 8-Core Results
[' Go run Sendmail.go ' | done:6.1073493s] Main OK0 4999999995000000001 4999999995000000006 4999999995000000008 4999999995000000003 4999999995000000009 499999999500000000
It can be seen that the execution time with 1 cores is 24 seconds, while the execution time of 8 cores is 6 seconds. The running time is greatly reduced. There is no lock function, so the order of their display is random. So there are some problems with the loss of print results