golang中并发实例

package main

import (
	"fmt"
	//"runtime"
	"os"
	"runtime/pprof" // 引用pprof package
	"time"
)

func main() {
	f, _ := os.Create("profile_file")
	pprof.StartCPUProfile(f)     // 开始cpu profile,结果写到文件f中
	defer pprof.StopCPUProfile() // 结束profile

	startTime := time.Now().Second()
	//runtime.GOMAXPROCS(runtime.NumCPU())

	ch := make(chan int, 100)
	quit := make(chan bool)

	go read(ch, quit)

	for i := 0; i < 2000; i++ {
		ch <- i
		fmt.Println(i)
	}

	quit <- true

	fmt.Println("\r\n====MAIN END====\r\n")
	endTime := time.Now().Second()

	fmt.Println(startTime)
	fmt.Println(endTime)
	fmt.Println("用时:", endTime-startTime, "秒")
}

func read(ch chan int, quit chan bool) {
	for {
		select {
		case value := <-ch:
			fmt.Print("----", value, "----")
		case <-quit:
			break
		}

	}
}