golang同時プログラミングテストコードコレクション

7029 ワード

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.
", 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...