To Thread or Not To Thread

Josh Deiner
Sep 2, 2018 · 4 min read

I think arguments are fun. Not the anger disguised by pleasantries or through shouting. Growing up, it always hurt my head to witness the hyper masculinity induced shouting matches where testosterone dictated the winner. Arguments should be able to mutually benefit both parties. This is only capable if we reject dogmatic approaches and consider that fun can be achieved if we attempt to defend our positions with fairness. How does this play into a tech blog? Online forums. Platforms like StackOverFlow and Reddit have introduced ways to communicate in an almost unfiltered capacity. Often you see posts meant to incite agitation. You may also see queries featuring people who are merely curious. As a former undergraduate Philosophy student, I think that Plato, regardless of the faults he had, was poignant in alluding that most people who proclaim supreme knowledge rarely are what they portray.

This is relevant in that I recently came across someone questioning the differences between Java and JavaScript(ES6). The question was how are they any different with the exception of being strongly typed. On the surface, this seems silly. In my mind, my immediate is response is wow that was odd. Java is a server side language. JavaScript is used for scripting. Obviously, I knew that in general Java is used to implement OOP and its surrounding features, eg. polymorphism.

The major issue here is that my conception of the real differences was weak. So I looked further into the primary differences on a core level. The rest of this blog will dive into the differences of JavaScript and Java in terms of threading.

One of the core differences seem to be threading. Generally, there is the question of whether a language is multi-threaded and single-threaded. Technically JavaScript is considered to be single-threaded. The best example I came across of multi-threading vs single threading in relation to a single processor is a skittle analogy.

Single Threaded :

  1. Reaching into a bowl of skittles, put skittles in mouth
  2. Eat skittles
  3. Repeat steps 1,2

Multi-Threaded

  1. Reaching into a bowl of skittles with left, put skittles in mouth
  2. Grab more skittles with right hand
  3. Finish eating skittles
  4. Put skittles from right hand into mouth
  5. Grab skittles with left hand
  6. Repeat…

When we think of multi-threading, we consider the callstack and a language’s runtime environment. Theoretically, if you are running Java you will have a main thread that will delegate different actions while it runs through its stack. This allows Java to work synchronously.

JavaScript is asynchronous. This is why a lot of new developers are often tested with closures and functions with timeout calls. This is a test of their understanding of scope.

Instead of being called a single-threaded language, JavaScript has been described as a single threaded asynchronous non-blocking language. Essentially this means that JS holds a stack, first-in last-out. In order to ensure that JS can operate with a modicum of speed callbacks are enabled. Functions within the stack reference APIs such as ajax with fetch. These will then transition a reference to the callback queque for the item in the particular item in the stack. These are then executed and the process repeats itself.

This can present several interesting develops not withstanding “callback hell”. If one of your callbacks were to recursively call itself indefinitely then you may encounter an issue in which further functions within the stack cannot be executed. This could make sense from a user/front-end perspective in which several factors are dependent upon each(user authentication and buying items), but could present issues from a backend perspective in terms scaling in that everything must be done linearly.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade