- الحصول على الرابط
- X
- بريد إلكتروني
- التطبيقات الأخرى
- الحصول على الرابط
- X
- بريد إلكتروني
- التطبيقات الأخرى
One Goroutine is always created automatically to execute the main method. So, even if you don't do anything special, GO Runtime will create a Goroutine to execute the main inside that Goroutine
Other Goroutines are created using a Go keyword, start a Goroutine it is pretty simple you say go, and then give the name of the function that you want to execute, and the Goroutine will execute the code associated with that function. A Goroutine generally exits when its code is complete
When the main Goroutine is completed all other Goroutines are forced to exit. So, that means, if you start with the main you can make several Goroutines from the main, but if even if these other Goroutines are not finished with their code, if the main Goroutine ends, then all the other Goroutines will be forced to end and will be forced to exit.
This is important to understand, to think about when you're writing your routines because what'll happen is, the Goroutine that you create may not complete its execution because the main completes first.
You might get this behavior where you say, "Look I wrote this code and this code to execute and it doesn't seem like it's executing." That might be because the main is completed before the Goroutine ever got to finish its own code.
Let’s See this example, we have two goroutines the first one is the main goroutine and print(“hello from the main goroutine”), the second one is created by using the go keyword and it calls function HelloFromAnothergoRoutine().
You expect to see two things printed on the screen, the main routine and the new routine, and you wouldn't necessarily know what order these two are going to get printed in. Right?
Because when the Goroutine is created, we don't know how the scheduler is going to schedule the main routine. Maybe it schedules the new Goroutine first maybe it schedules the main Goroutine first, we can't really tell. But at a glance, you would expect it to print the main routine and print a new routine at some point in some order.
But when I execute this, only the main routine is printed. Why does this happen?
because the main Goroutine is finishing before the other new Goroutine.
This happens consistently. So, theoretically, we don't know how the schedule is going to work. The Go runtime scheduler. We don't know is it going to let the main Goroutine run first, or the new Goroutine run first we don't know.
Theoretically, if you run this over and over, you could get different ordering, right? You would think well, sometimes the main goes first, so it does the main routine and then it quits. Sometimes it does the new Goroutine then prints a new routine and main routine. Right? Maybe
What I assume from this is that the Go runtime scheduler is giving preference to the main routine. So it's always lighting the main routine go first before the new Goroutine. I don't know that for a fact, but it seems like that's how it's implemented.
But even if that's how it's implemented we have no definite knowledge of that. Meaning, maybe it's implemented in that way right now, but then in the next version of Go, they might change the scheduler so it does something different.
- الحصول على الرابط
- X
- بريد إلكتروني
- التطبيقات الأخرى
تعليقات
إرسال تعليق