Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7195617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:36:50+00:00 2026-05-28T20:36:50+00:00

Why synchronized keyword does not create monitor enter at byte code level every time

  • 0

Why synchronized keyword does not create monitor enter at byte code level every time I use it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:36:51+00:00Added an answer on May 28, 2026 at 8:36 pm

    The synchronized keyword can be used in two ways:

    1. When you use synchronized(obj) inside the body of a function, the compiler will emit monitorenter/monitorexit bytecodes for the relevant monitor.

    2. If the entire method is declared synchronized, in the bytecodes the method will be marked as ACC_SYNCHRONIZED. The JVM will implicitly enter and exit the monitor when entering/exiting the method. No monitorenter/monitorexit bytecodes are emitted, nor indeed required.

    Consider the following two methods:

    public class Sync {
    
        public void f() {
            synchronized (this) {
            }
        }
    
        public synchronized void g() {
        }
    
    }
    

    They compile to:

    public void f();
      Code:
       0:   aload_0
       1:   dup
       2:   monitorenter
       3:   monitorexit
       4:   return
    
    public synchronized void g();
      Code:
       0:   return
    

    As you can see, g() is still marked as synchronized in the bytecodes, so the JVM knows what to do.

    Disclaimer: This is what my compiler does. It seems possible that a different compiler might chose to emit monitorenter/monitorexit instead of using ACC_SYNCHRONIZED. Whether any existing compiler does that, I don’t know.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first time using the synchronized keyword, so I am still unsure
Does c# have its own version of the java synchronized keyword? I.e. in java
When should we use ReentrantReadWriteLock as compared to synchronized keyword in multithreaded environment in
When we say we lock on an object using the synchronized keyword, does it
If I want to lock the whole array I can use the synchronized keyword
Is using the 'synchronized' keyword on methods in a Java DAO going to cause
Does ruby have the Java equivalent of synchronize keyword? I am using 1.9.1 and
I'm having issues with Synchronized not behaving the way i expect, i tried using
My question refers to whether or not the use of a ReentrantLock guarantees visibility
The Java documentation says: it is not possible for two invocations of synchronized methods

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.