DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Using Python Libraries in Java
  • Designing a Java Connector for Software Integrations
  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs

Trending

  • Java's Quiet Revolution: Thriving in the Serverless Kubernetes Era
  • How to Build Local LLM RAG Apps With Ollama, DeepSeek-R1, and SingleStore
  • Build Your First AI Model in Python: A Beginner's Guide (1 of 3)
  • Grafana Loki Fundamentals and Architecture
  1. DZone
  2. Coding
  3. Java
  4. Multithreading Java and Interviews Part 2: Mutex, the Java Monitor Model

Multithreading Java and Interviews Part 2: Mutex, the Java Monitor Model

In the second installment of Multithreading and Interviews, read an introduction to the basics of Java Multithreading with Mutex, the Java Monitor Model.

By 
Dmitry Egorov user avatar
Dmitry Egorov
DZone Core CORE ·
Updated Oct. 14, 21 · Tutorial
Likes (24)
Comment
Save
Tweet
Share
26.7K Views

Join the DZone community and get the full member experience.

Join For Free

In the previous chapter, we learned about the following:

Shared variable

Also in the previous article, we faced a problem with a shared variable that shouldn't be shared or at least one that we have to define the order for updating this variable.

Java Monitor Model: Both Great and Terrible

Java provides many ways to order access to shared data between threads; here, we start from the most primitive but pretty popular (in legacy projects) way to do it. Java Monitor Model is a model that decides how threads communicate with each other and provides synchronization keywords.

Synchronization Keywords: Synchronize, Wait, and Notify

You might have already noticed that each object in Java has the methods "wait" or "notify". Those keywords are used for synchronization. Each object in Java can be used as a synchronization object, also known as Mutex.

Hidden Java Monitor

Java Monitor Model is like a phantom that orchestrates the threading order.  Let's take a look into this next example below:

Hidden Java monitor code image.

You can see that inside the method doPrintSequentially, we perform print function surrounded by synchronized(MUTEX) construction. By wrapping code with that Mutex, we give access to that area only 1 thread at a time.

Revealing the Hidden Synchronization Part

We can't say how precisely Java handles synchronization. Generally, it replaces synchronization keywords by a special condition that checks that the current Mutex is taken by some thread.  So if we rewrite the previous code, it might look like this:

Previous code rewritten image

Basically, it puts all threads in a blocking state until running one release Mutex by completing the synchronization part or changing its state to "WAITING".

Java Monitor Rules

  • Mutex belongs only to one thread at a time in a synchronized area (or many areas).
  • Once threads leave the synchronized area, it releases the Mutex.
  • The thread can call the wait function and set itself into a WAITING state and it will release Mutex automatically. 
  • Thread(s) with WAITING state can change its state to BLOCKING by received notification from another thread but with the same Mutex.
  • The thread can notify other WAITING thread(s), but this doesn't release the Mutex automatically.
  • All threads with BLOCKING state will acquire Mutex once it's released, but in random order (not LIFO OR FIFO order).

Blocking Thread Safe Queue Implementation

Blocking Thread Safe Queue Implementation code image

Threads to Run: John and Peter Threads

This Thread Safe Queue will be executed by 2 threads, John and Peter. John will try to get data from the queue and will be blocked because it's empty. The Peter thread will start 1 second later and push a new record in the queue and notify the blocked John thread. 

Thread safe queue

10 Acts

Now, let's describe the process of getting and pushing data in a thread safe queue or drama in 10 acts.

Stage 1.

Stage 3.

Stage 4.

Stage 5.

Stage 6.

Stage 7.

Stage 8.

Stage 9.

10 stages

So, in this example, we described the communication between 2 threads using the keywords synchronize, wait, and notify. But, what if we start more than 1 getter thread? Well, in that case, the situation will be more complex. We will need to use the notify all synchronization feature in the next installment. 

Java (programming language) Monitor (synchronization)

Opinions expressed by DZone contributors are their own.

Related

  • Using Python Libraries in Java
  • Designing a Java Connector for Software Integrations
  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!