Go and main thread are in the process of seizing

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Recently learning the Go language, just learned the association process this piece, encountered a confused place. This is the official go language document. In my understanding is that the process can only be released in the main thread after the time slice will go through the system scheduling to run the process, in fact, the correct is indeed true, but I have encountered the main process of the process of a problem, after help, has now been understood. No more nonsense, read the code First

1 Package Main2 3 Import (4"FMT"5"Time"6 )7 8 Func Main () {9Go Say ("World")TenSay ("Hello") One     /* A FMT. Println ("---------------1") -  - A: = []int{7, 2, 8,-9, 4, 0} the FMT. Println ("= = =", A[:len (a)/2]) - c: = make (chan int) - Go sum (A[:len (a)/2], c) - Go sum (A[len (a)/2:], c) + x, y: = <-c, <-c//Receive from C -  + FMT. Println (x, y, x+y) A  at FMT. Println ("---------------2") -  - C2: = Make (chan int, 2) - C2 <-1 - C2 <-2 - FMT. Println (<-C2) in FMT. Println (<-C2) -  to FMT. Println ("---------------3") + C3: = Make (chan int, ten) - Go Fibonacci (Cap (C3), C3) the For I: = range C3 { * FMT. Println (i) $         }Panax Notoginseng  - FMT. Println ("---------------4") the C4: = make (chan int) + Quit: = make (chan int) A go func () { the For I: = 0; i < ten; i++ { + FMT. Println (<-C4) -             } $ quit <-0 $         }() - Fibonacci2 (C4, quit) -  the FMT. Println ("---------------5") - tick: = time. Tick (+ * time.millisecond)Wuyi boom: = time. After ($ * time.millisecond) the For { - Select { Wu Case <-tick: - FMT. Println ("tick.") About Case <-boom: $ FMT. Println ("boom!") - return - Default: - FMT. Println (".") A Time . Sleep (* time.millisecond) +             } the         }*/ - } $  theFunc Say (sstring) { the      forI: = 0; I < 5; i++ { the          Time.Sleep(100 * Time.millisecond) theFmt.Println (s) -     } in}

See the results of the code run two times first

First: ( see Print Order in conjunction with the above code )

Second time: ( combined with first view print order )

Did you find out that every time the print order was different?

This is the process of seizing execution.

Let's take a look at it. Sequential, main thread run ====> release time slice ====> run ==> release time slice ====> main thread run

According to this code

1 Say ("Hello")

We know that this is part of the main thread, so priority execution ( Note that the argument is "Hello")

And look at the say method.

1 string {2      for I: = 0; i < 5; i++ {3time         .  Sleeptime. millisecond) 4         Fmt. Println (s) 5     }6 }

When executed into the loop inside the

time. Sleep time. Millisecond)

The time slice is freed while the execution code is paused, the system schedules to the co-

Go Say ("World")

is also the same method that also executes

time. Sleep time. Millisecond)

Release time slices

So when you print it, it's going to happen.

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.