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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency
  • Jakarta EE 12: Entering the Data Age of Enterprise Java

Trending

  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Building a Spring AI Assistant With MCP Servers: A Step-by-Step Tutorial
  • Advanced Error Handling and Retry Patterns in Enterprise REST Integrations
  • Persistent Memory for AI Agents Using LangChain's Deep Agents
  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
27.4K 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

  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency
  • Jakarta EE 12: Entering the Data Age of Enterprise Java

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook