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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL
  • Member Spotlight: Shai Almong

Trending

  • The Window Closes: Why I Switched to Kotlin Before the Codebase Got Too Big
  • PostgreSQL vs Oracle: When Higher Cost Means Better Performance
  • Java 8 Optional - Avoid Null and NullPointerException Altogether - and Keep It Pretty
  • Who Should Grade Your AI? Rules vs LLM Judges vs Humans

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
DZone Core CORE ·
Updated Oct. 14, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
27.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL
  • Member Spotlight: Shai Almong

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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