Multithreading Java and Interviews Part 1: An Introduction
This article analyzes the reason why so many developers hate completing multithreading tasks during interviews and how you can combat this daunting task.
Join the DZone community and get the full member experience.Join For Free
Why Do So Many Devs Hate Completing Multithreading Tasks During Interviews?
The key reason is that multi-threading not widely used in production. Most of the problems can be solved without multithreading. For example, one way is by using a database as a referee or some queues (Java Messaging Pattern). So, what do you really need to know? You need to remember the theory of Synchronisation Monitor and related things, but you almost never use it on a daily working basis.
Is There Any Way to Escape From the Multithreading Question?
Unfortunately, no; there is no escape. Multithreading is and will be one of the most important Java parts, and many companies might check how confident you are in this area, even if it's not a part of their ecosystem. Maybe, in the future, Java will release more transparent models than the existing Java Monitor, but I would rather believe that market would migrate to Kotlin with its coroutines.
Passing Multithreading Tasks Is Often the Main Selection Criterion For Hiring Senior Programmers
As far as multithreading questions, they are more complex, and many interviewers use them as filters to find "real pro" developers, and it's such a shame. Initially, Java conquered the world as a production language due to its simplicity and maintainability over C++. Nowadays, most of the problems can be solved using application servers with an embedded thread pool. So, it's pretty unexpected to see any synchronization keywords and practices in most of the java projects. And, it's pretty reasonable because any code with manual synchronization quickly complicates the system and makes the code unmaintainable.
So, How Do You Solve Multithreading Tasks Even Without a Lot of Experience?
First of all, it makes sense to learn basic things by heart such as multithreading like Java Monitor Model and so on. After that, you can learn classical problems by heart, which will give you a good advantage in interviews.
The Beginning: Learning About Multithreading.
The key purpose of multithreading java API is to let developers make interactions between many independent activities (threads). Practically, it's about providing access in the right order to thread for the given resource. Maybe it sounds easy, but developers struggle with such tasks.
Making Multithreading Your Friend Again
In this article series, I'll try to make multithreading more clear and transparent for you.
So, Let's Start From the Very Beginning...
Let's take a look at the simple code below:
Single thread imaging is pretty simple, but when we run the same code with 2 threads, JVM sees that code differently like so:
As you can see, each has 2 instances of the doSomething method, and we still have only 1 shared variable: latestTime. So, without arranging the proper access order, we might have unexpected behavior. In our case, we might print latestTime updated in another thread.
To solve this problem there are many ways. Find out how in the second part of this series.
Opinions expressed by DZone contributors are their own.