`
qq466862016
  • 浏览: 125696 次
  • 来自: 杭州
社区版块
存档分类
最新评论

golang并发编程测试代码收藏

阅读更多
package channel

import (
  "fmt"
  "time"
  "sync"
)

type People struct {
	
	Name string
	Id int
	Age int
	Sex string 
	State bool
}


/**main函数调用此方法**/
func Start() {
	
	go fmt.Println("good gogogog !")
	fmt.Println("golang")
	time.Sleep(1 * time.Second)
	//goroutine
	
	Publish("大家好我叫冬天",time.Second * 3)
	
	time.Sleep(1 * time.Second)
	fmt.Println("Ten seconds later: I’m leaving now.")
	TestChannel()
	fmt.Println("all finish...")

}



func Publish(text string ,delay time.Duration) {
	go func () {
		
		  time.Sleep(delay)
		  fmt.Println("breaking news: ", text)
		}()
}


func TestChannel() {
	
//	lc :=make(chan int)
	//wc :=make(chan bool,10)
	ch :=make(chan People)
	
	//p := People{"冬天",101,100,"男",true}
//	pch :=make(chan *People)
	
   go addPeople(ch)
   
   
   for v := range ch {
   	   fmt.Println("People is : ",v)
   }
	
	TestClose()
	
	TestRange()
	TestWait()
	TestRace()
	TestRace()
	TestMutex()
	TestWaitGroup()
	TestSelect()
	TestLast()
}



func addPeople (ch chan People ) {
	p := People{"冬天",101,100,"男",true}
	ch <- p
	close(ch)
}


func  TestClose() {
	ch := make(chan string)
	
	go func(){
		ch <- "冬天"
		close(ch)
	}()
	
	fmt.Println(<-ch)
	fmt.Println(<-ch)
	fmt.Println(<-ch)
	fmt.Println(<-ch)
	v,ok :=<-ch
	
	fmt.Println("ok=",ok," v =",v)
}



/**
 * 测试range channel 
**/
func TestRange() {
	
	ch := make(chan int)
	go RangeInfo(ch)
	
	for v :=range ch {
		fmt.Println("======= v= =====",v)
	}
	
}


func RangeInfo(ch chan int) {
	fmt.Println("我要休息...",time.Now().Format("2006-01-02 15:04:05"))
	time.Sleep(1 * time.Second)
	 ch <- 100
	 fmt.Println("我要休息...",time.Now().Format("2006-01-02 15:04:05"))
	 time.Sleep(1 * time.Second)
	 	 ch <- 101
	 	 fmt.Println("我要休息...",time.Now().Format("2006-01-02 15:04:05"))
	 time.Sleep(1 * time.Second)
	 	 ch <- 102
	 	 fmt.Println("我要休息...",time.Now().Format("2006-01-02 15:04:05"))
	 time.Sleep(1 * time.Second)
	 	 ch <- 103
	 	 fmt.Println("我要休息...",time.Now().Format("2006-01-02 15:04:05"))
	 time.Sleep(3 * time.Second)
	 	 ch <- 104
	 	 fmt.Println("我要休息...",time.Now().Format("2006-01-02 15:04:05"))
	 time.Sleep(1 * time.Second)
	 ch <- 105
	 	 close(ch)
}


func TestWait() {
	
	ch := TestWaitInfo()
	
	fmt.Println("你就在这等待吧.....")
	<-ch
	fmt.Println("等待结束....")
	
}


func TestWaitInfo() (wait <- chan struct{}) {
	
	ch := make(chan struct{})
	go func(){
		fmt.Println("我要暂停了...")
		time.Sleep(2* time.Second)
		 close(ch)
		fmt.Println("我要暂停了...")
	}()
	
	return ch
}




func TestRace() {
	
	
	wait := make(chan struct{})
	 n :=0
	 go func() {
	 	 n ++
	 	 close(wait)
	 }()
	 
	 n ++
	 <- wait
	 fmt.Println(n)
}


func SharedIsCaring() {
	
	ch := make(chan int)
	go func(){
		n :=0
		n ++
		ch <-n
	}()
	
	n := <- ch
	n++ 
	fmt.Println(n)
}

 type AtomicInt struct {
 	mu sync.Mutex
	n int
 }
 
 func(a *AtomicInt) Add(n int) {
 	  a.mu.Lock()
 	  a.n +=n
 	  a.mu.Unlock()
 }
  func(a *AtomicInt) Value()( int) {
 	  a.mu.Lock()
 	   n := a.n
 	  a.mu.Unlock()
 	  return n 
 }
 
func TestMutex() {
	  wait :=make(chan struct{})
	  var n AtomicInt
	  go func(){
	  	 n.Add(1)  //一个访问
	  	 close(wait)
	  }()
	  n.Add(2)  //另外一个访问
	  <-wait
	  fmt.Println(n)
	  
	  
	  
}

func TestWaitGroup() {
	
	var wg  sync.WaitGroup
	wg.Add(5)
	
	for i :=0;i<5;i++ {
		 n :=i
		go func(){
			   fmt.Println(n)
			   wg.Done()
			}()
	}
	
	wg.Wait()
}

func TestSelect() {
	
	ch := make(chan int)
	ch2 := make(chan bool)
	
	go func(){
		
		ch <- 100
		close(ch)
		ch2 <-true
		close(ch2)
	}()
	
	select {
		case v,ok:=<-ch:
		fmt.Println("===========",ok,v)
		case v,ok:=<-ch2:
		fmt.Println("ch2  ",v,ok)
	}
	
	
}



func TestLast() {
	people := []string{"冬天", "春天", "夏天", "秋天", "四季"}
	match := make(chan string,1)
	wg := new(sync.WaitGroup)
	wg.Add(len(people))
	
	
	for _,name:=range people{
		go Seek(name,match,wg)
	}
	
	wg.Wait()
	
	select {
		case name:=<- match:
		fmt.Println("接收到数据为 : ",name)
		default:
		
	}
}

func Seek(name string,match chan string,wg *sync.WaitGroup) {
	
	
	select {
		case peer:= <-match:
		 fmt.Printf("%s sent a message to %s.\n", peer, name)
		 case match <-name:
	}
	wg.Done()
	
}

 

package main

import(
	  "fmt"
	  "channel"
	)



func main(){
	
	fmt.Println("===============")
	channel.Start()
	
}

 

===============
golang
good gogogog !
Ten seconds later: I’m leaving now.
People is :  {冬天 101 100 男 true}
冬天



ok= false  v = 
我要休息... 2016-01-20 15:13:28
我要休息... 2016-01-20 15:13:29
======= v= ===== 100
breaking news:  大家好我叫冬天
我要休息... 2016-01-20 15:13:30
======= v= ===== 101
我要休息... 2016-01-20 15:13:31
======= v= ===== 102
我要休息... 2016-01-20 15:13:32
======= v= ===== 103
我要休息... 2016-01-20 15:13:35
======= v= ===== 104
======= v= ===== 105
你就在这等待吧.....
我要暂停了...
我要暂停了...
等待结束....
2
2
{{0 0} 3}
1
2
0
4
3
=========== true 100
四季 sent a message to 冬天.
春天 sent a message to 秋天.
接收到数据为 :  夏天
all finish...

 

2
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics