Go-1의 동시성

Go is a concurrent language and not a parallel one. Before discussing how concurrency is taken care in Go, we must first understand what is concurrency and how it is different from parallelism.동시성이란 무엇입니까?
동시성은 한 번에 많은 것을 처리할 수 있는 기능입니다. 예를 들어 가장 잘 설명되어 있습니다.
Let's consider a person jogging. During his morning jog, let's say his shoelaces become untied. Now the person stops running, ties his shoelaces and then starts running again. This is a classic example of concurrency. The person is capable of handling both running and tying shoelaces, that is the person is able to deal with lots of things at once :)
동시성과 병렬성 - 기술적 관점



큰 프로그램은 더 작은 하위 프로그램으로 나뉩니다. 더 작은 구성 요소를 동시에 실행하는 프로그램을 동시성이라고 합니다.

고루틴

A goroutine is a lightweight execution thread in the Go programming language and a function that executes concurrently with the rest of the program.Goroutines are incredibly cheap when compared to traditional threads as the overhead of creating a goroutine is very low. Therefore, they are widely used in Go for concurrent programming.To invoke a function as a goroutine, use the go keyword.통사론
가 푸()We write go before the function foo to invoke it as a goroutine. The function foo() will run concurrently or asynchronously with the calling function.
예시:

package main
import (
    "fmt"
    "time"
)

// Prints numbers from 1-3 along with the passed string
func foo(s string) {
    for i := 1; i <= 3; i++ {
        time.Sleep(100 * time.Millisecond)
        fmt.Println(s, ": ", i)
    }
}

func main() {

    // Starting two goroutines
    go foo("1st goroutine")
    go foo("2nd goroutine")

    // Wait for goroutines to finish before main goroutine ends
    time.Sleep(time.Second)
    fmt.Println("Main goroutine finished")
}


예시:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    ch := make(chan string)

    for i := 0; i < 10; i++ {
        go func(i int) {
            for j := 0; j < 10; j++ {
                ch <- "Goroutine : " + strconv.Itoa(i)
            }
        }(i)
    }

    for k := 1; k <= 100; k++ {
        fmt.Println(k, <-ch)
    }
}


https://replit.com/@neerajkumar1997/Goroutines?v=1

채널에서 값 보내기 및 받기
주요 기능에는 두 개의 기능 생성기와 수신기가 있습니다. c int 채널을 만들고 생성기 함수에서 반환합니다. 채널에 값을 쓰는 익명 고루틴 내에서 실행되는 for 루프 c.

package main

import (
    "fmt"
)

func main() {
    c := generator()
    receiver(c)
}

func receiver(c <-chan int) {
    for v := range c {
        fmt.Println(v)
    }
}

func generator() <-chan int {
    c := make(chan int)

    go func() {
        for i := 0; i < 10; i++ {
            c <- i
        }
        close(c)
    }()

    return c
}


https://replit.com/@neerajkumar1997/Send-and-Receive-values-from-Channel?v=1

여러 고루틴 시작하기

package main

import (  
    "fmt"
    "time"
)

func numbers() {  
    for i := 1; i <= 5; i++ {
        time.Sleep(250 * time.Millisecond)
        fmt.Printf("%d ", i)
    }
}
func alphabets() {  
    for i := 'a'; i <= 'e'; i++ {
        time.Sleep(400 * time.Millisecond)
        fmt.Printf("%c ", i)
    }
}
func main() {  
    go numbers()
    go alphabets()
    time.Sleep(3000 * time.Millisecond)
    fmt.Println("main terminated")
}


https://replit.com/@neerajkumar1997/Starting-multiple-Goroutines?v=1



고루틴은 여기까지입니다. 좋은 하루 보내세요.

좋은 웹페이지 즐겨찾기