Coder Social home page Coder Social logo

produce_consumer_with_blocking_queue_compare's Introduction

三种语言的简单生产者消费者模型的实现

在研究Kettle时, Kettle的实现原理让我眼前一亮:

  • 在ETL流程图中, 所有Step同时开始(而不是我认为的一个接一个)
  • 每个Step执行一样的操作:先读取上游Hop是否有RowSet, 如果有,则读取并转换,转换的结果写到下游的Hop
  • Hop有容量概念,比如一个Hop如果容量是1000,那么当容量为0时,下游Step取数时就会发生阻塞。同理,当容量为1000时,上游Hop取数时会发生阻塞

那么,问题是:Hop是怎么做到阻塞的呢?
答案就是Hop中维护了一个BlockingQueue, 恰好实现了上面的需求。\

于是看了java的BlockingQueue, 顺带比较了一把3种语言的BlockingQueue实现 \

java版

Kettle是用的 java版实现,采用LinkedBlockingQueue
基于线程进行阻塞,明显效率低
run: java BlockingQueueExample.java

rust版

rust是基于通道(channel)实现,虽然示例中采用了thread,但明显没有必要,因为关键数据结构sync_channel并不基于thread
rust可以提供更好的性能
run: rustc blocking_queue_example.rs

go版

go是基于go channel, 与rust在同一水准。
不过直观感觉, go在写这种程序时,明显更易用

produce_consumer_with_blocking_queue_compare's People

Contributors

g770728y avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.