Coder Social home page Coder Social logo

threads-with-nodejs's Introduction

threads-with-nodejs

Thiw project is mean to better understand how to do multithreating with nodejs 12.10.0. For this, i installed htop to be able to keep track of the processes and threads.

Before running my node program, i went ahead and (through htop) filter my processes in a tree mode as well as searching for the program i'm running which is node index.js. Right away i can see that nothing is displayed:

alt text

Then i went ahead and implemented a simple program which seems to run a single thread with only one worker and this is what it looked like:

alt text

As you can see, it spun up a thread with 6 additional workers on top of the one i needed to run. Seeing this, I was currious what would happened if I added 2 more workers.

alt text

Interestingly enough, it seems to always add 6 additional workers and this seems to be a patter even when i add 12 workers:

alt text

Order of execution completion

Now, i was curious to see how would the workers finish if i wasn't to have a setTimeout so here's the result i've got after a few tries:

Try #1

$ node index.js 
waiting for worker thread0 1 to finish!
false
waiting for worker thread2 3 to finish!
false
waiting for worker thread3 4 to finish!
false
waiting for worker thread4 5 to finish!
false
waiting for worker thread6 7 to finish!
false
waiting for worker thread9 10 to finish!
false
waiting for worker thread11 12 to finish!
false
waiting for worker thread1 2 to finish!
false
waiting for worker thread8 9 to finish!
false
waiting for worker thread5 6 to finish!
false
waiting for worker thread7 8 to finish!
waiting for worker thread10 11 to finish!
false
false

Try #2

$ node index.js 
waiting for worker thread4 5 to finish!
false
waiting for worker thread8 9 to finish!
false
waiting for worker thread3 4 to finish!
waiting for worker thread5 6 to finish!
false
waiting for worker thread0 1 to finish!
false
waiting for worker thread1 2 to finish!
false
waiting for worker thread2 3 to finish!
false
false
waiting for worker thread6 7 to finish!
false
waiting for worker thread10 11 to finish!
waiting for worker thread11 12 to finish!
false
waiting for worker thread7 8 to finish!
false
waiting for worker thread9 10 to finish!
false
false

Try #3

$ node index.js 
waiting for worker thread0 1 to finish!
false
waiting for worker thread1 2 to finish!
false
waiting for worker thread6 7 to finish!
false
waiting for worker thread2 3 to finish!
false
waiting for worker thread9 10 to finish!
waiting for worker thread3 4 to finish!
false
false
waiting for worker thread5 6 to finish!
false
waiting for worker thread7 8 to finish!
waiting for worker thread10 11 to finish!
false
false
waiting for worker thread8 9 to finish!
false
waiting for worker thread4 5 to finish!
false
waiting for worker thread11 12 to finish!
false

threads-with-nodejs's People

Contributors

opposite-bracket 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.