go concurrency

concurrency

package main

import (
	"fmt"
	"time"
)

func main() {
	go Go()
	time.Sleep(2 * time.Second)

}

func Go() {
	fmt.Println("Go Go Go!!!")
}

使用chan

package main

import (
	"fmt"
)

func main() {
	c := make(chan bool)
	go func(){
		fmt.Println("Go Go Go!!!")
		c <- true
	}()
	<-c
}

for 版本

package main

import (
	"fmt"
)

func main() {
	c := make(chan bool)
	go func() {
		fmt.Println("Go Go Go!!!")
		c <- true
		close(c)
	}()

	for v := range c {
		fmt.Println(v)
	}
}

有缓存和没有缓存的 chan

package main

import (
	"fmt"
)

func main() {
	c := make(chan bool)
	//c := make(chan bool, 1)
	go func() {
		fmt.Println("Go Go Go!!!")

		<-c
	}()
	c <- true
}

多个协程

package main

import (
	"fmt"
)

func main() {
	c := make(chan bool)
	for i := 0; i < 10; i++ {
		go Go(c, i)
	}
	<-c
}

func Go(c chan bool, index int) {
	a := 1
	for i := 0; i < 10000000; i++ {
		a += i
	}
	fmt.Println(index, a)
	if index == 9 {

		c <- true
	}
}

10个缓存并使用 多核的版本

package main

import (
	"fmt"
	"runtime"
)

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	c := make(chan bool, 10)
	for i := 0; i < 10; i++ {
		go Go(c, i)
	}
	for i := 0; i < 10; i++ {
		<-c
	}
}

func Go(c chan bool, index int) {
	a := 1
	for i := 0; i < 10000000; i++ {
		a += i
	}
	fmt.Println(index, a)
	c <- true

}

wait group 版

package main

import (
	"fmt"
	"runtime"
	"sync"
)

func main() {

	runtime.GOMAXPROCS(runtime.NumCPU())
	wg := sync.WaitGroup{}
	wg.Add(10)
	for i := 0; i < 10; i++ {
		go Go(&wg, i)
	}
	wg.Wait()

}

func Go(wg *sync.WaitGroup, index int) {
	a := 1
	for i := 0; i < 10000000; i++ {
		a += i
	}
	fmt.Println(index, a)
	wg.Done()

}

select

package main

import (
	"fmt"
)

func main() {

	c1, c2 := make(chan int), make(chan string)
	o := make(chan bool, 2)
	go func() {
		for {
			select {
			case v, ok := <-c1:
				if !ok {
					fmt.Println("c1 !ok")
					o <- true
					break
				}
				fmt.Println("c1", v)
			case v, ok := <-c2:
				if !ok {
					fmt.Println("c2 !ok")
					o <- true
					break
				}
				fmt.Println("c2", v)
			}
		}
	}()

	c1 <- 1
	c2 <- "hi"
	c1 <- 3
	c2 <- "hello"

	close(c1)
	close(c2)

	for i := 1; i < 2; i++ {
		<-o
	}
}

使用 select 发送

package main

import (
	"fmt"
)

func main() {

	c := make(chan int)

	go func() {

		for v := range c {
			fmt.Println(v)
		}
	}()
	for {
		select {
		case c <- 0:
		case c <- 1:
		}
	}

}

chan 超时

package main

import (
    "fmt"
    "time"
)

func main() {
    c := make(chan bool)
    select {
    case v := <-c:
        fmt.Println(v)
    case <-time.After(3 * time.Second):
        fmt.Println("timeout")
    }
}

创建一个 goroutine,与主线程按顺序相互发送信息若干次并打印

package main

import (
    "fmt"
)

var c chan string

func Pingpong() {
    i := 0
    for {
        fmt.Println(<-c)
        c <- fmt.Sprintf("From Pingpong:Hi, #%d", i)
        i++
    }
}

func main() {
    c = make(chan string)
    go Pingpong()
    for i := 0; i < 10; i++ {
        c <- fmt.Sprintf("From main: Hello,#%d", i)
        fmt.Println(<-c)
    }
}