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.

    斯是陋室惟汝德馨!欢迎来访!
  • 相关阅读:
    js中的回调函数的理解和使用方法
    js循环的总结
    jquery选择器
    Jquery的命名冲突
    ul+li标签制作表格
    MyEclipse代码提示功能和自动提示功能
    a configuration error occured during startup.please verify the preference field with the prompt:
    MyEclipse2014,java文件无法编译,run as上是none applicable,不是文件本身的问题
    Myeclipse自定义注释
    Run As none applicable
  • 原文地址:https://www.cnblogs.com/yuanmh/p/15085762.html
Copyright © 2011-2022 走看看