Deep Dive Into Java Concurrency
This is the start of a series where I explore the nuances of Java concurrency and how it plays a part in enterprise/large-scale applications.
While I work with Java daily, the issue of concurrency doesn’t always come up. When it does, however, it is a tricky subject and requires a lot of careful navigation to make sure the application is as efficient and operational as possible (follows CAP Theorem).
Originally, computers were built to run one program at a time. One CPU would handle a process, and a queue of processes would be worked on one by one. To solve this, the concept of multitasking was introduced with one CPU running multiple programs at a time by switching between processes very quickly, which looked like zero downtime to the user.
When multithreading came around, it pushed the idea even further. Each CPU can run multiple programs/processes, but with multiple threads of execution inside each program. This allows a program such as a word checker to analyze your words while you type inside the editor.
As technology advanced, it was possible to have many CPUs in one computer, or multiple cores in one CPU. This horizontally scaled the number of threads, allowing for a large number of processes to be handled seemingly simultaneously (but it was more just an optimized handing off of available workers to simulate a no lag environment).
Multithreading is so important because it not only allows users to continue utilizing the application with no downtime, but it is extremely efficient in allowing other processes to be worked on while a CPU/core is idle. It would be a waste of CPU cycles if we just left the core sitting in downtime rather than letting it work on another process before switching back. This means the CPU is always chipping away at possible jobs.
Source: Java Concurrency and Multithreading - Introduction https://www.youtube.com/watch?v=mTGdtC9f4EU&list=PLL8woMHwr36EDxjUoCzboZjedsnhLP1j4&index=1&pp=iAQB
However, multithreading is not without its qualms. Things like race conditions, invisible writes, congestion, deadlock, etc. are all likely issues that will arise. There are ways to remedy, but it requires careful implementation.