Thursday, January 14, 2021

What is Java Memory Model (JMM)?

The Java memory model (JMM) describes how threads in the Java 
programming language interact through memory.

The Java memory model was the first attempt to provide a comprehensive memory model for a popular programming language and was justified by the increasing prevalence of concurrent and parallel systems, and the need to provide tools and technologies with clear semantics for such systems. 

The above sounds very complicated so let's break it down into layman's terms!

From my previous posts I think we can agree that computers and what they do can be understood on a very basic level - however, the complexity which results from the processor being able to do many things at once (at least, it appears that way to us humans) means that we have to manage that complexity.

Producing a feasible model allows us to interact with the concepts and processes that are part of any programming language. Also, giving stuff acronyms makes you look very clever! 

On modern platforms, code is frequently not executed in the order it was written - this does not make sense to our logical brains because we tend to want a direct line of thought, i.e start, continue then end.

Before your game loads stuff happens in the background - a bit like looking at the end of a non-fiction book and using the index listing to see where the interesting things are, your program first gets compiled. Now here's where it's interesting: we would put a book together with the intro page first, the chapters in sequential order and then at the end have the reference section.

A computer doesn't see life this way! It goes and looks for the juicy bits first before reading the novel and remembers where they were and what they do. Then it reads through volumes of only moderately interesting pages and doesn't care about what order they are in! This is called compiling which comes from the Latin verb compīlō, meaning "heap" or "plunder".

This way, maximum performance can be achieved - the JMM defines the allowable behavior of multithreaded programs by placing constraints on the way the threads and the memory interact with each other.

There are voluminous reference works available on this subject, all very in-depth and not needed here where I am giving an overview of this concept. For further reading, please browse Wikipedia and where you find some new insight, dive right in and explore!

Now let's pay cat tax - here is a word salad of some of the interesting terms associated with JMM:)



No comments:

Post a Comment