zoukankan      html  css  js  c++  java
  • Generator and Consumer model for Goland Go语言-生产者消费者模式demo

    Generator and Consumer model

    Golang is the right language for such a language model. Because goroutine is designed for such multiple thread/routine to run the job.

    And at the same time, golang owns channel struct to make it possible for different threads/routines to communicate with each others.

    The following is the simple example to show how to use golang to complement a generator-consumer-model.

    main function

    const (
        goroutionNum = 100
    )
    
    func main(){
    	glog.V(3).Infof("[BenchMark]:starting to test")
    	startTime := time.Now().UTC().UTC()
    	ch := make(chan string, 100)
    	done := make(chan bool, goroutionNum)
    	for i := 1; i <= goroutionNum; i++ {
    		go Consumer(i, ch, done)
    	}
    	go Producer(ch)
    	for i := 1; i <= goroutionNum; i++ {
    		<-done
    	}
    
        // analyse the result
    	endTime := time.Now().UTC().UTC()
    	totalDuration := endTime.Sub(startTime).Minutes()
    	glog.V(3).Infof("[BenchMark] Result for %s---------------
    ", time.Now().UTC().UTC().String())
    	glog.V(3).Infof("[BenchMark]:goroutine count: [%d], Total duration is %d mins
    ", goroutionNum, totalDuration)
    	return nil
    }
    

    generator function

    // here you can write your code to generate items
    func Producer(ch chan string) error {
    	for i:=0;i<10000;i++{
    	    ch <- fmt.Sprintf("item #%d",i )
    		}
    	}
    	close(ch)
    	return nil
    }
    

    consumer

    func Consumer(id int, ch chan string, done chan bool) {
    	for {
    		value, ok := <-ch
    		if ok {
    			// Simulation process
    			fmt.Printf("I am work #%d, I am handling %s",id, value)
    		} else {
    			break
    		}
    	}
    	done <- true
    }
    
    

    OK, thanks for your view.

    斯是陋室惟汝德馨!欢迎来访!
  • 相关阅读:
    HDU 4472 Count DP题
    HDU 1878 欧拉回路 图论
    CSUST 1503 ZZ买衣服
    HDU 2085 核反应堆
    HDU 1029 Ignatius and the Princess IV
    UVa 11462 Age Sort
    UVa 11384
    UVa 11210
    LA 3401
    解决学一会儿累了的问题
  • 原文地址:https://www.cnblogs.com/yuanmh/p/15085762.html
Copyright © 2011-2022 走看看