📜  高朗 | Goroutine vs 线程

📅  最后修改于: 2021-09-12 11:19:59             🧑  作者: Mango

Goroutine: Goroutine 是一个函数或方法,它与您程序中存在的任何其他 Goroutines 相关联地独立并同时执行。或者换句话说,Go 语言中每个并发执行的活动都称为 Goroutines。

线程:进程是操作系统的一部分,负责执行应用程序。在您的系统上执行的每个程序都是一个进程,为了在应用程序内部运行代码,进程使用称为线程的术语。线程是一个轻量级的进程,换句话说,线程是一个执行程序下代码的单元。所以每个程序都有逻辑,一个线程负责执行这个逻辑。

以下是 Goroutine 和 Thread 之间的一些区别:

Goroutine Thread
Goroutines are managed by the go runtime. Operating system threads are managed by kernal.
Goroutine are not hardware dependent. Threads are hardware dependent.
Goroutines have easy communication medium known as channel. Thread doesnot have easy communication medium.
Due to the presence of channel one goroutine can communicate with other goroutine with low latency. Due to lack of easy communication medium inter-threads communicate takes place with high latency.
Goroutine doesnot have ID because go doesnot have Thread Local Storage. Threads have their own unique ID because they have Thread Local Storage.
Goroutines are cheaper than threads. The cost of threads are higher than goroutine.
They are cooperatively scheduled. They are preemptively scheduled.
They have fasted startup time than threads. They have slow startup time than goroutines.
Goroutine has growable segmented stacks. Threads doesnot have growable segmented stacks.